Can't set pointer to derived type

I’m making a test pawn class, and creating a component inside.
In the header file I put:

UPROPERTY(EditAnywhere)
USceneComponent* mySceneComponent;	

and in the cpp:

AToonCarPawn::AToonCarPawn()
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
	mySceneComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OurVisibleComponent"));

}

But i get the error :a value of type “UStaticMeshComponent *” cannot be assigned to an entity of type “USceneComponent *”

Doesn’t UStaticMeshComponent inherit USceneComponent? Am I misunderstanding something? I am perplexed.

Just to clarify, that’s not ALL that’s in those files. I just included the relevant snippets, and didn’t realize it would put line numbers like that. Let me know if I’m leaving out anything important.

Hi Zachfm,

you must use the exact class type for every member that are flagged as UPROPERTY, otherwise the Blueprint magic won’t work.

This is inherent to the way UE4 macro work: UPROPERTY will parse the next line and generate blueprint access (read/write…) functions according to the class.

Hope it helps,

Is that true in all cases, or only for properties which are flagged as accessible to Blueprint?

It looks like it’s only for UPROPERTY, the error must come for the UBT

What Co said is not true. You can assign any subclass of a variable type to the variable and the UPROPERTY will still function.

Have you tried casting the result of the component creation to USceneComponent* ?

I tried an explicit static cast and it said there was “no known conversion”

Thanks for your answer, Co

I was following this little tutorial when I did this: https://docs.unrealengine.com/latest/INT/Programming/Tutorials/PlayerInput/1/index.html

It looks like they do exactly this in the third step of the tutorial. Maybe I’m missing a setting somewhere or some nuance that’s allowing them to do this? Is there any difference between their code and mine that i’m not realizing?

You can try this (but it’s ugly):

mySceneComponent = Cast<USceneComponent>(CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OurVisibleComponent")));

I tried the exact code you have above and it compiled and runs fine.
This was as a member variable with parent class UActorComponent;
Perhaps it needs a clean + build so the generated file is recreated? The only other thing that comes to mind is it’s somehow picking up a override, perhaps with a default template parameter?
Mac10.11.2,Xcode7.2,Unreal 4.10.2