[SOLVED] Using Casts From Child Classes

Hello,

I have an issue casting a class then using the cast in a child class, here is some pseudo-code to explain:

In the header file of the class Im casting in:

UPROPERTY(EditDefaultsOnly, Category=MyCategory)
class TSubclassOf OtherClass;

protected:
UPROPERTY(EditDefaultsOnly, Category=MyCategory)
class AMyOtherClass* Other;

In the class Im casting in:

OtherClassClass = AMyOtherClass::StaticClass();

FActorSpawnParameters SpawnInfo;
AMyOtherClass* Other = GetWorld()->SpawnActor(OtherClass, SpawnInfo);
if(Other)
{
...
}

Now that cast works perfectly fine, but then I go into the child class and type

if(Other)
{
...
}

It fails. Am I doing something wrong when casting? Or is there something special I need to do to access it from the child class?

Thanks!
~Adam

I believe you are shadowing your variable Other.

You’ve made a local stack variable called Other in that example, hiding the member variable.

AMyOtherClass* Other = GetWorld()->SpawnActor(OtherClass, SpawnInfo);
if(Other)
{
...
}

should be

Other = GetWorld()->SpawnActor(OtherClass, SpawnInfo);
if(Other)
{
...
}

Thanks man! It works now, I really appreciate the help! :smiley: