() does return NULL

Hi!

I created a new class that inherits UObject. When I try to call () it return null. How can I fix that?

auto World = ();
if (World != NULL)
{
	World->SpawnActor<ATestProjectile>(ProjectileClass, position, rotation);
}

Edit: I create the object I call this code from in the following way if that matter:

NewObject<UTemplateWeapon>();

If () returns null it means object or world it self is not fully initiated at point it’s being called, i suspect you place it in constructor in which that usually happens, so you need to place it in some event function when everything is ready.

A UObject does not know inherently what world it belongs to. The purpose of the function is for that Object to be able to let others know which World it is in. Actors do this via their Level reference and Components do this either via their Actor owner or the World they’ve been registered with. Each other Object type has to find its own path back to World, often through a reference to an Actor.

If you want to be able to call on your class, you need to override the function and provide a valid path to a World.

Hmm, I actually call it in an event function… no idea why it doesn’t work :confused:

Thanks! That solved it.