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.


#pragma once

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

class ASB_BaseGameMode : public AGameMode

	UColourModel* ColourModel;

	UColourModel* getColourModel();


#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;


enter code here#pragma once

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

class UColourModel : public UObject

	// Array of all instantiated SpellColour objects
	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:


bump. I’m having a similar issue.

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



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!).