Hello all, How to create a cube in scene with Blueprint or C++?

Hello all, How to create a cube in scene with Blueprint or C++ ?

Hello, 袁涛

First thing you need to implement this in C++ is to declare an appropriate field in your class, which will look something like this:

UPROPERTY()
TSubobjectPtr<UBoxComponent> Cube;

After this, please don’t forget to initialize your cube in constructor of the class and configure in needed way:

Cube = ObjectInitializer.CreateDefaultSubobject<USkeletalMeshComponent>(this, TEXT("Cube"));
Cube->bHiddenInGame = false;
RootComponent = Cube;

Finally, you spawn the cube:

GetWorld()->SpawnActor(ACube::StaticClass());

Please note, that ACube should be replaced with the name of your Cube class.
If you like to learn how to do this using Blueprints, this tutorial should help you:
https://docs.unrealengine.com/latest/INT/Gameplay/HowTo/SpawnAndDestroyActors/Blueprints/index.html

Hope this helped!

If you just want to spawn a Cube mesh in a level then in blueprint you can do:

  1. “SpawnActorFromClass” node
  2. Assign it the Class “StaticMeshActor”
  3. Plug a make transform node into the SpawnTransform property
  4. Plug the return value into a SetMesh function of its StaticMeshComponent
  5. Set the NewMesh property to a cube mesh. Several come with the Engine content

Thank you, it really helped.

I tried this as this seems to be the easiest to test. I added a key event as a trigger to the Spawn Actor. However, I keep getting “Warning Mobility of StaticMeshActor_24 : StaticMeshComponent0 has to be ‘Movable’ if you’d like to move.”, and no cube was spawned