Calling C++ function on spawned actor

This should be fairly basic, but I’m trying to Spawn a C++ actor, then call a function right after it’s created. A much simplified version of my code is as follows:

Object.h

UCLASS()
class MK1_API AObject : public AActor
{

// Other Things

public:

    UFUNCTION()
        void Initialize(void);

// Other things
}

Object.cpp

// Other things

void AObject::Initialize()
{
// Does nothing for the moment
}

Creator.cpp

#include "Object.h"
    
// Other things
    
    
void UCreator::BeginPlay()
{
    // Get ToSpawn, spawnLocation, rotator, spawnParams - these are all ok.

    AActor* Obj = World->SpawnActor<AActor>(ToSpawn, spawnLocation, rotator, 
    spawnParams);

    if(Obj)
    {
     Obj->Initialize(); // This fails    
    }
}

Basically when I try to access Initialize from the pointer to Obj, it doesn’t recognise the function. I feel like, from the error I’m getting, that the pointer doesn’t point to the actual instance of the class…

Thanks for the help.

Your object, AOther, inherits AActor. When you spawn in UCreator::BeginPlay, you are telling it to spawn AActor specifically, not AOther, so as far as Obj is concerned, it’s an AActor, which has no knowledge of your Initialize method.

What you should do is change the spawer so it spawns an object of type AOther:

AOther* Obj = World->SpawnActor<AOther>(ToSpawn, spawnLocation, rotator, 
     spawnParams);

You might want to call Super::BeginPlay in your UCreator::BeingPlay method too (if it is overriding BeginPlay that is).

Thanks for the quick answer! Makes sense, and it now works!