GetOwner() Not Working?

I’m creating an actor component that needs to get the UPrimitiveComponent of the owning actor, but it keeps giving me this error : A value of type “UActorComponent *” cannot be used to initialize an entity of type "UPrimitiveComponent *

This is my code :
TSubclassOf<UPrimitiveComponent> Class;
UPrimitiveComponent* FloatingObject = GetOwner()->GetComponentByClass(Class);

I’ve tested the code in blueprints and I’m converting it to C++, but I’m confused at why the GetOwner is not letting me get a primitive component.

If you look at GetComponentByClass() declaration, you’ll see that it returns UActorComponent*, not UPrimitiviComponent*; The engine can’t implicitly convert it.
Try casting to UPrimitiveComponent first.

1 Like

How would I cast to a UPrimitiveComponent? Sorry, still new to C++.

UPrimitiveComponent* FloatingObject = Cast<UPrimitiveComponent>(GetOwner()->GetComponentByClass(Class));

Don’t forget to check if (FloatingObject != nullptr) after.

The cast keeps failing even though I copied your code. What should I do?

Does GetOwner() return anything? What component is that? Is that a primitive component at all?

Get Owner() works by itself. This code is in an actor component. Hope that helps

Unless I’m missing something, PrimitiveComponent and ActorComponent don’t seem to be in the same hierarchy?

How would I get the primitive component of the owner then? I created it in blueprints first to make sure everything works and it does, but I’m stuck on getting the primitive component.

OK, so, I was misunderstanding – per the manual, Primitive is a child of Scene which is a child of Actor.

OK, so, what is GetComponentByClass returning? You can put a breakpoint in the debugger where you are querying it, and see what sort of a value you are getting out of it. If you get something that isn’t a PrimitiveComponent back, casting it to PrimitiveComponent will return null. If you get null back, casting it will return null. So you need to find out what you are getting back before casting.

Also, “the” primitive component doesn’t entirely make sense – an actor can have many components.

So, the original error is because the function returns an ActorComponent, which is not necessarily a PrimitiveComponent, it could be a SceneComponent, or an ActorComponent. You’re getting something other than a PrimitiveComponent back.

I need the primitive component because it applies to anything that has geometry, and that is how I can add force at location to a component that is a static or skeletal mesh. At the start of the game the game randomly chooses a mesh that is either skeletal or static. I used a primitive component because it works for both (At least in my blueprint test). If you know a better way could you please share?

I can’t debug it for you – do

UActorComponent* Temp = GetOwner()->GetComponentByClass(Class);

set a breakpoint on it, and see what Temp contains. You’re not able to cast it to PrimitiveComponent because whatever you’re getting back is not a primitivecomponent. Figure out what you are getting, and that might help inform us further. :slight_smile:

It looks like it is returning an actor component. I’ve never debugged that way so I’m not 100% sure.

yes the pointer will be of type UActorComponent, since that’s what GetComponentByClass returns, but if you examine the component further, you should be able to determine what kind of component it actually is, and figure out why GetComponentByClass is returning that first instead of what you’re expecting.

It works by itself