ACharacter->GetMesh() failing to return skeletal mesh

I’m playing around with the third person C++ demo project, and trying to get a reference to the ThirdPersonCharacter BP’s skeletal mesh, so I can run AttachTo:

 UMNHitbox::UMNHitbox(){
    	sockSphere = CreateDefaultSubobject<UStaticMeshComponent>(FName(TEXT("TestSphere")));
    
    	ACharacter* myCharacter = (ACharacter*)this->GetOwner();
    	
    	if (myCharacter != nullptr)
    	{
    		if (myCharacter->GetMesh() != nullptr)
    		{
    			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "GetMesh != nullptr!");
    			sockSphere->AttachTo(myCharacter->GetMesh(), "RightHandHB_Socket");
    		}
    	}
    }

This looks right to me, but GetMesh() returns null every time I run it- I’ve been poring over this, but I can’t see where I’m fudging up. The only line I can think of that might be explicitly wrong is the cast, as I mess up casting syntax a lot- is there another obvious problem with my implementation?

You should be using a different casting approach, do it like this:

 ACharacter* myCharacter = Cast<ACharacter>(GetOwner());

Also make sure you actually have an Owner Set. If you debug, do you see a return value from GetOwner? Hope that helps =)

Ooh that makes a boatload of sense, fixing the cast made everything work; thank you!! :slight_smile: