Setting UObject as a UProperty

Hi, so I seem to be having an issue with setting UObjects as UPropertys, I listed two experiments here as they are related and I assume I am making the same mistake both times. The only part I have gotten to work properly is using TSubclass as a property, then creating the object from there. So I have listed below the experiments I did:

The variable is set up in the AGameMode subclass using the following code:

UPROPERTY(EditAnywhere)
class USquadHub* SquadHub;

The USquadHub clas is declared as:

UCLASS(Blueprintable, BlueprintType, DefaultToInstanced, EditInlineNew)
class TMO_API USquadHub : public UObject

From reading the class specifiers page, I thought DefaultToInstanced and EditInlineNew would create an instance of the object if I assigned the USquadHub class to the SquadHub variable. I can assign the variable:

224174-squadhub-dropdown.png

However as soon as the blueprint compiles the value is lost and reverts back to this

224175-squadhub-dropdownnone.png

The other thing is creating an instance of the squadhub in the constructor of the object which maintains the pointer to the squad hub object. I assumed this would set a default squadhub object. So in the constructor for the gamemode:

ATMOGameMode::ATMOGameMode()
{
	SquadHub = NewObject<USquadHub>();
}

The DefaultToInstanced and EditInlineNew UClass modifiers were dropped:

UCLASS(Blueprintable, BlueprintType)
class TMO_API USquadHub : public UObject

So same issue again, straight after the compile of the C++ code

224176-squadhub-no-plain.png

However again after the compile

224177-squadhub-no-plain.png

So I am assuming the cause of this is the same issue, any help on this would be greatly appreciated.

Thanks so much for any help

Hi MechaFarin,

NewObject<USomeUObject>() is used to dynamically initialize objects during runtime. You should not use it in the constructor of your classes, especially within the constructor of your GameMode. What happens here is that your object gets garbage collected before you can even see it. You can verify this by using an IsValid node with the BeginPlay event. You might argue that you have marked it with UPROPERTY() and that it should not be automatically garbage collected, but as far as I understand, that’s for the case when these objects are initialized during the runtime. It’s also a good practice to always provide the Outer parameter of NewObject<USomeUObject>(/*Outer Parameter Here*/) to properly link your object to the World and be able to access the World. You can simply do this by passing this as its outer parameters such as NewObject<USomeUObject>(this). Note that if you do this in the constructor of your GameMode you will crash the engine. Instead, initialize it in the overridable method InitGame(...) of your GameMode and then verify that it is valid and working.

If you want to initialize any class which directly or indirectly inherits from UObject in the constructor of your classes, then you should use CreateDefaultSubobject<USomeUObject>(TEXT("ObjectName")) or other similar factory methods. Try this and you’ll see that your object will not disappear when you compile your code or blueprint. You can also add all those class specifiers as well and it will still work fine :slight_smile:

Hope this helps.