Change Location of actors spawned through C++

I was able to spawn the cubes in Run-time through C++, but I can’t change their location. The function SpawnActor creates the object, but does not change its location, and the method setActorLocation returns false. Does Anybody know why ? Thanks !

void AMyGameMode::BeginPlay()
{
Super::BeginPlay();
AMazeWall test;
FActorSpawnParameters parameters;
parameters.Name=TEXT(“MazeWall”);
UWorld
const World = GetWorld();
FVector vector(120.0,-260.0,340.0);
FRotator rotator(20,20,20);

if (World)
{
    test = World->SpawnActor<AMazeWall>(AMazeWall::StaticClass(), vector, rotator, parameters);
    
    if(!test->SetActorLocation(vector))
        cout << "False";
}

}

So, where does it get spawned? At 0,0,0? Does it set the rotation correctly?

that right, it gets spawned at 0, 0, 0, and I can’t change its rotation either

Sorry for just seeing this question now.

I’m guessing that the problem is NOT that you cannot set the actor location. I’m guessing that the problem is that AMazeWall does not have a RootComponent set (to a UBoxComponent or something).

If AMazeWall.cpp does not have something like this in your constructor (or it’s set somewhere else):

BaseCollisionComponent = PCIP.CreateDefaultSubobject<UBoxComponent>(this, TEXT("BaseBoxComponent"));
BaseCollisionComponent->SetBoxExtent(FVector(125.f, 125.f, 10.f), true);
RootComponent = BaseCollisionComponent;

Then you cannot move the actor. I believe it will actually crash the editor if you try.

So yeah, make sure there’s a subcomponent that RootComponent is assigned to.

Did you get this problem resolved? I have the same problem now and get stuck.

Does your actor have a component that is set as root?

Yes, it has. Actually, I find that i can change the rotation. I mistaken the units of the angle as radian measure. :slight_smile: