Why is my pointer to a UObject in Gamemode getting assigned to null?

Hi people, I have an odd problem here.

I was in need of a static/singleton class that I could access from anywhere on the server, and after researching a little bit I decided to store a reference to a UObject derived class in the Gamemode.

SB_BaseGameMode.h:

#pragma once

#include "ColourModel.h"
#include "GameFramework/GameMode.h"
#include "SB_BaseGameMode.generated.h"

/**
 * 
 */
UCLASS()
class ASB_BaseGameMode : public AGameMode
{
	GENERATED_UCLASS_BODY()

	UPROPERTY()
	UColourModel* ColourModel;

	UColourModel* getColourModel();
	
};

SB_BaseGameMode.cpp:

#include "Spellbraid.h"
#include "SB_BaseGameMode.h"


ASB_BaseGameMode::ASB_BaseGameMode(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	ColourModel = NewObject<UColourModel>();
}

UColourModel* ASB_BaseGameMode::getColourModel()
{
	return ColourModel;
}

ColourModel.h;

enter code here#pragma once

#include "Object.h"
#include "BaseSpellColour.h"
#include "ColourModel.generated.h"

UCLASS(Blueprintable)
class UColourModel : public UObject
{
	GENERATED_UCLASS_BODY()

	// Array of all instantiated SpellColour objects
	UPROPERTY()
	TArray<UBaseSpellColour*> colours;

	// Gets the SpellColour object of the specified class
	UFUNCTION(BlueprintCallable, Meta = (DisplayName = "Get SpellColour"), Category = "Colours")
	UBaseSpellColour* getSpellColour(TSubclassOf<class UBaseSpellColour> key);

};

The issue is that, while ColourModel gets set up in the constructor of the Gamemode:

+		this	0x0000000018f3c400 {ColourModel=0x0000000018dc7500 {colours={AllocatorInstance={Data=0x000000001cca5fc0 {...} } ...} } }	ASB_BaseGameMode *

The ColourModel pointer seems to be null whenever I access the Gamemode after it’s constructed:

ASB_BaseGameMode* mode = World->GetAuthGameMode<ASB_BaseGameMode>();

+		mode	0x0000000018f3c400 {ColourModel=0x0000000000000000 <NULL> }	ASB_BaseGameMode *

The address for the mode is the same, which leads me to believe that there’s a problem with the ColourModel pointer.
I thought it was getting garbage collected at first, but it is a UPROPERTY() and I have also tried AddToRoot(), which didn’t help.

Anybody have an idea what’s going on? D:

Thanks

bump. I’m having a similar issue.

Did you ever manage to solve this? I’m having the exact same problem right now.

Try

ConstructObject<UColourModel>(UColourModel::StaticClass()); 

insted of NewObject

Also constructor is only for defaults, im not sure if it good place initiation of object (Definitly not for actor spawning), try place it on BeginPlay()

's suggestion to move creation of the UObject to “Begin Play” instead of the constructor worked for me.

In hindsight I think the point that the constructor is the wrong place for doing this makes sense, it seems like setting up actor components and the like is what that is used for (please correct me if I’m mistaken!).