# Unreal engine 4 - Actor not updating (when instancing on c++)?

My custom actor (containing a few custom UMeshComponent) does not update when I change its location or the visibility of its components.

I instantiate it this way in StartPlay() of my GameMode class:

  MyCustomActor = GetWorld()->SpawnActor<AMyCustomActor>(AMyCustomActor::StaticClass());


Then I set its new location like this:

  MyCustomActor->SetActorLocation(FVector(100,0,0));


Is it because I did not set the Owner? Or what is wrong?

Note

I don't understand why, but my code works now... It might be linked to those warnings:

LogActor:Warning: GameSession /Game/PersistentLevel.GameSession_19 has natively added scene component(s), but none of them were set as the actor's RootComponent - picking one arbitrarily 

and

LogActor:Warning: Natively added component (MyCustomActor_143.RootComponent) was left unattached from the actor's root.

• So the actor spawns correctly, just subsequent changes aren't applied to it? Could you include the constructor for AMyCustomActor. You shouldn't need to specify an Owner to move an actor about. – T. Kiley Sep 15 '15 at 21:24
• I don't understand why, but my code works now... I think it has to do with some warnings: LogActor:Warning: GameSession /Game/UEDPIE_0_ProBuilderLevel.ProBuilderLevel:PersistentLevel.GameSession_19 has natively added scene component(s), but none of them were set as the actor's RootComponent - picking one arbitrarily  and LogActor:Warning: Natively added component (Gizmo_143.RootComponent) was left unattached from the actor's root. – arthur.sw Sep 16 '15 at 10:51
• Yeah, so when you add components in C++ to your actors, you need to assign on of them to RootComponent. I guess you used to be using an older version of Unreal which was silently failing, but now it is randomly choosing one of your components to be the root. (I didn't know they'd fixed that - I used to get caught out by this all the time!) – T. Kiley Sep 16 '15 at 12:47

The problem is that in the constructor of your "MyCustomActor" you need to assign a RootComponent that is a SceneComponent. Typically you will see something like a ShapeComponent (SphereComponent, CylinderComponent, etc.).

Then in the constructor, once you have a RootComponent defined in the actor, you need to attach the RootComponent to all other SceneComponents that you add to that Actor.

Constructor Example:

AMyActor::AMyActor()
{
BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxCollider"));
// ... setup your box component here ...
RootComponent = BoxComponent;

StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
// ... setup your static mesh component here ...
StaticMeshComponent->AttachTo( RootComponent ); // Or you could write StaticMeshComponent->AttachTo( BoxComponent );
}


By setting this as the RootComponent, and assuming your Actor is set to Movable, is active, and allowed to update, your call to SetActorLocation(FVector(100, 0, 0)); will move your actor to that location.