Different Meshes for a Actor instances

Hey guys! I have a class “CityBuildings”, which is basically just a mesh of some buildings. Now, I have multiple meshes that I would like to display randomly. I spawn a 10x10 array of CityBuildings and don’t want to look every instance the same. So, is there a way to randomly pick between multiple meshes when the object is spawned? I thought I could just use a rand() function in the constuctor, but then all instances get the same mesh (the one, which was selected during the first constructor call).

Then I thought about don’t using a CityBuildings class at all, since it doesn’t have any parameters except the mesh/components. But I also failed to add mesh components procedurally at runtime. The Buildings should be added to a city class. I load the staticMeshes in the constructor of the city-class and then at runtime try to spawn a UStaticMeshComponent and set this meshes as the corresponging static meshes. But I either don’t see anything (nothing appears), or the code crashes in the RegisterComponent function…


	

for (int i = 0; i < gridWidth; i++){
		for (int j = 0; j < gridHeight; j++){
			if (buildingGrid->cells*[j].isBuildable == true){

				UStaticMesh* randMesh = NULL;

				int randMeshIndex = rand() % 3;

				if (randMeshIndex == 0) randMesh = habitations2;
				if (randMeshIndex == 1) randMesh = habitations3;
				if (randMeshIndex == 2) randMesh = habitations4;
				if (randMeshIndex == 3) randMesh = habitations1;

				UStaticMeshComponent* comp = NewObject<UStaticMeshComponent >();
				comp->SetStaticMesh(randMesh);

				int rot = rand() % 360;
				comp->AddLocalRotation(FRotator(0, rot, 0));
				comp->SetWorldScale3D(FVector(25, 25, 25));
				buildingGrid->cells*[j].setAndOrientateBuilding(comp);

				UStaticUtilityFunctions::SetupSMComponentsWithCollision(comp);

				comp->RegisterComponentWithWorld(GetWorld());
			//	comp->RegisterComponent();
				comp->AttachTo(RootComponent);
				comp->SetVisibility(true);

				AddOwnedComponent(comp);
				RegisterAllComponents();

			}
		}
	}



Dont do it in the constructor, do the random generation in BeginPlay(); The constructor is not a good place for dynamic code in UE4, best for static things.

BeginPlay gets called separately for each instance, so it should return different rand results.

But I can’t use Construction Helpers outside the constructor?!