# UObject pointer going null on the second launch in the editor

**URL:** <https://forums.unrealengine.com/t/uobject-pointer-going-null-on-the-second-launch-in-the-editor/878844>\
**Category:** C++\
**Tags:** unreal-engine, crash, CPP, question\
**Created:** [April 8, 2023, 5:31pm UTC](https://forums.unrealengine.com/t/uobject-pointer-going-null-on-the-second-launch-in-the-editor/878844 "2023-04-08T17:31:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![malfrax](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@malfrax](https://forums.unrealengine.com/u/malfrax)\
**Post date:** [April 8, 2023, 5:31pm UTC](https://forums.unrealengine.com/t/uobject-pointer-going-null-on-the-second-launch-in-the-editor/878844/1 "2023-04-08T17:31:32Z")

</div>

Hello,

I’m setting up the game instance class in order to manage room generation.  
I have two objects, URoom and UBiome that are UObject blueprint implementable that define the logic for generating the world.  
I’m setting up two pointers on my GameInstance class and I have a function InitializingBiome that generate them by calling NewObject.

When I launch the first time I launch the editor everything is fine. When I play it a second time, my two objects are fine on the initialization (InitializeBiome() and GenerateRoom() functions) but when I call the function GetEnemiesToSpawn() the object become invalid. Like if the objects was garbage collected but I didn’t missed UPROPERTY. I tried using TSoftObjectPtr with no success.

My class is setup like this :

DungeonGameInstance.h

```auto
UCLASS()
class CUBEFACTORYGAME_API UDungeonGameInstance : public UGameInstance
{
	GENERATED_BODY()

public:
	UDungeonGameInstance();
	virtual void Init() override;
	
public:
	UFUNCTION(BlueprintCallable)
	void InitializeBiome();
	UFUNCTION(BlueprintCallable)
	void GetEnemiesToSpawn(TArray<UEnemyData*> &OutEnemies);
	UFUNCTION(BlueprintCallable)
	void GetSlots(TArray<USlotData*> &Slots);
	UFUNCTION(BlueprintCallable)
	void GetChoicesForNextRoom(TArray<FRoomInfo> &OutChoices);
	UFUNCTION(BlueprintCallable)
	URoom *GetCurrentRoom();
	UFUNCTION(BlueprintCallable)
	void GoToNextRoom(int Choice);
	UFUNCTION(BlueprintCallable)
	void GenerateEndBattle();

protected:
	UFUNCTION(BlueprintCallable)
	void GenerateRoom(const FRoomInfo &Info);
	
	UPROPERTY(VisibleAnywhere)
	URoom *CurrentRoom;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
	UBiome *CurrentBiome;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
	TArray<FRoomInfo> NextChoices;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
	int CurrentRoomId;
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
	TArray<TSubclassOf<UBiome>> Biomes;
	UPROPERTY(VisibleAnywhere)
	int CurrentBiomeId;
};

```

DungeonGameInstance.cpp

```auto
void UDungeonGameInstance::InitializeBiome()
{
	if (Biomes.Num() > CurrentBiomeId)
	{
		CurrentBiome = NewObject<UBiome>(this, Biomes[CurrentBiomeId].Get());
		CurrentBiome->GenerateRoomPattern();
        // CurrentBiome is valid here
		TArray<FRoomInfo> Infos = CurrentBiome->GetRoomPatternFromId(0);
		if (Infos.Num() > CurrentRoomId)
		{
			GenerateRoom(Infos[CurrentRoomId]);
		}
	}
}

void UDungeonGameInstance::GenerateRoom(const FRoomInfo& Info)
{
	CurrentRoom = NewObject<URoom>(this, Info.RoomClass.Get());
	CurrentRoom->SetType(Info.Type);
	CurrentRoom->GenerateRoomAssets();
    // CurrentRoom is valid here.
}

void UDungeonGameInstance::GetEnemiesToSpawn(TArray<UEnemyData*> &OutEnemies)
{
	if (CurrentRoom == nullptr) // At the second time, CurrentRoom is not null but is an invalid pointer
	{
		return;
	}
	if (!CurrentRoom->IsEnemyGenerated())
	{
		CurrentRoom->GenerateEnemyPool();
	}
	if (!CurrentRoom->Enemies.IsEmpty())
	{
		OutEnemies.Append(CurrentRoom->Enemies); // Crash here on the append.
	}
}

```

The debugger on CurrentRoom inside the function GetEnemiesToSpawn():  
 ![CurrentRoomSecondTIme](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/6/f/26fb0c6d8bcee9009c9f3d355dbe4c85a6397405.png)

Maybe I’m missing something about the gameinstance? I search around internet and the documentation but found nothing explaining my problem.

Thanks in advance if you can help me on that !

---

<div class="post-metadata">

**Author:** ![malfrax](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@malfrax](https://forums.unrealengine.com/u/malfrax)\
**Post date:** [April 8, 2023, 6:15pm UTC](https://forums.unrealengine.com/t/uobject-pointer-going-null-on-the-second-launch-in-the-editor/878844/2 "2023-04-08T18:15:57Z")

</div>

I’m an idiot. I stored on a different function my game instance into a static variable. When I restarted, the value was invalid. Don’t put your uobject on static variable and everything should be fine if you have the same error…
