Casting to change transform and getting nullptr

I am trying to implement a teleport ability for my player character that has him teleport to the position of a projectile he shoots. I have it all coded and ready so that when the projectile is shot and the player right clicks on the mouse, they use the teleport function on the projectile and the projectile then casts to the player character, allowing it to change the player’s transform to be equal to that of he projectile. However, it seems that when I do this I end up with a nullptr and I inevitably crash.

This is in my player character header

void Ability();

AProtoCharacter* playerActor;

AProtoTProjectile* ProtoTBall;

I am calling this function through a BindAction in the player character cpp.

void AProtoCharacter::Ability()

This is in my projectile header.

void BallTele(class AActor* playerActor);

Finally, this is how I’m casting back to my player character from the projectile cpp. I only crash if I remove the if statement that checks if the playerActor is a nullptr.

void AProtoTBall::BallTele(class AActor* playerActor)
    AProtoCharacter* player = Cast<AProtoCharacter>(playerActor);

    UE_LOG(LogTemp, Warning, TEXT("Working"));

if (player != nullptr)
    UE_LOG(LogTemp, Warning, TEXT("player pointer not nullptr"));

if (playerActor != nullptr)
    UE_LOG(LogTemp, Warning, TEXT("Has Teleported"));

I’ve tried having the key input be taken by the projectile instead but that doesn’t work.

The only other alternative I can think of is making it so that the Teleportation function on the projectile grabs its own transform, and when the player’s script calls over to it to just make the player characters transform equal the projectiles within the player characters script, instead of having the projectile change the player’s transform.

Any and all help would be greatly appreciated! I’m trying to get more experienced with C++ as I’m jumping to Unreal over from Unity. I have so much to learn, but I’m enjoying it!

Thanks in advance!

Hi, is AProtoCharacter extending an AActor class/child class?

Edit: if it’s compiling that would probably be the case. New question - is the playerActor variable set when you pass it to the BallTele function?


AProtoCharacter is extending. It does compile. It only crashed when I try to call the function (I right click) while running the code.

Yes, the variable is set when I pass it into BallTele. I am however unsure if I am not setting it in the correct place, or if the cast is what is causing the nullptr perhaps.

You have to set playerActor to something before you call Ability

 void AProtoCharacter::Ability()

this give you a reference to yourself, which would be your ProtoCharacter

Doing this also crashes the program. The crash log stating EXCEPTION_ACCESS_VIOLATION.

It crashes earlier in code, on the Ability function call.