Randomising your character’s Skeletal Mesh on spawn in Unreal Engine

- by

I have a simple class that spawns x amount of enemy characters into my world, but by default they all use the same skeletal mesh. I wanted Unreal Engine to use a different mesh from a random pool of characters. I’ve used an array for this, create a random integer not larger than the array, then use the construction script to swap out the mesh. Here’s how I did it.

Creating the Array

An array in Unreal Engine is created as a regular variable of the type you need. For my purposes, I needed one of type Skeletal Mesh Actor. Once created, take a look at the details panel and see the little icon next to the type. Click it and select Array.

For any default values to be settable, we need to compile the blueprint. This will show us that our array is empty (i.e. it has no elements). Click the unsuspecting plus icon to add one or several, then choose values for each one.

To make this easy for our meshes, you can select an item in the content browser, then click the little arrow in a circle icon. Alternatively you can click a field and browse. Here’s what my array looks like with 5 mesh choices.

Picking a random mesh

I’ll use a function so I can randomize the mesh any time I feel like it, this way I can either call it from Begin Play or the construction script.

Here I grab a reference to my array, then query its length and use a Random Integer node to pick a number between 0 and the size of my array. In our case this should return a number between 0 and 4, and we can use the result to retrieve an element from the array. We’ll use hat to set a new Skeletal Mesh Asset on our current mesh, which results in a random mesh swap every time this function is called.



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment