How to reference a UObject in scene and modify its properties at runtime in C++?

Hey Ghost

In Unreal it’s the same as in Unity.

You have a public Variable which can be edited in Editor.

In Unreal you have to specify the visibility of a Variable through the UPROPERTY macro.

So:

//.h
void DoAThing();

UPROPERTY(EditAnywhere, Category = "MyCategory")
UObject* ObjectToReference;

//.cpp
void Class::DoAThing()
{
    if(IsValid(ObjectToReference)
    {
        //...Do stuff with ObjectToReference. 
        //(GetComponents etc. is only available when UObject is an AActor)
    }
}

You can reference “ObjectToReference” by dragging the Object which implements DoAThing into the Scene and filling
“Object To Reference” in the Details Panel.

But be aware that UObject don’t have any Components except they are AActor.
So if you just want a reference to an actual Actor use instead of UObject* AActor*

I hope I could help you :slight_smile:

Good Luck