Instanced Variables can't be casted

Here I declared an instanced variable like so:

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Instanced)
class UItem* Item;

I want to grab the “Heal Amount” variable, but upon casting to BP Item Food, which is what holds the HealAmount variable, the cast fails.

Why do instanced variables even give the option to use a child class if you can’t even cast them into the child class? This doesn’t seem to make any sense. Perhaps I am using this incorrectly?

285353-capture.png

Any help would be appreciated, this is preventing my entire system from working currently.

Regards,
Reuben

If ‘Item’ was of type ‘BP Item Food’ or higher this would work and you could pull ‘get heal amount’ from the blue pin, but apparently is isn’t?..

Exactly, the item is definitely of type BP_Item_Food, however the cast seems to fail using an instanced variable.

I have fixed it but this solution isn’t what fixed it. I just restarted the editor and it somehow worked. Love your website btw, has helped me plenty in my Unreal journey :slight_smile:

Your exposed property is a class reference and not an object reference. I think this is why your type cast fails.

Can you try the following code?

//In your header file
UPROPERTY(EditAnywhere)
TSubclassOf<class UItem> Item;

//To access the heal amount
if(Item)
{
     float HealAmount = Item->GetDefaultObject<UItem>()->HealAmount;
}

Thanks, I really appreciate it!

I have fixed the issue, I just restarted the editor or something and now it works… weird :stuck_out_tongue: