How to create a random texture on spawn in world

There is a very easy and performant way to do that !
Let me show you :slight_smile:

  • First, create the material. Connect a “TextureSampleParameter2D” to the diffuse slot.
  • Then, create the blueprint. Add a new “Texture2D” variable and set it as “Array”. Add as many textures as you want in this array.
  • Create a random Integer In Range, with the amount of textures you set in your variable.
  • Now, create a DynamicMaterialInstance of your Material from your first step, and load the random texture.

Here is a quick example:

On the left, the blueprint; on the right the material.

2 Likes

Hello all, having a few issues with this one, what i’m trying to do is have a single static mesh (a ship) and have multiple textures for that mesh, (currently have 10), when the object spawns in the world it will randomly pick one of the textures, so that each different object has a different look to it (hope that explains it), the type of game i’m making is a turret defense game, so multiple instances of the same mesh are spawned i just want to have them look different when they spawn into the world :slight_smile: thank you in advance

A network of If nodes and a scalar parameter in the material should do the trick.

Setup your textures like so with If nodes, and use a scalar parameter to switch between them(the node on the left end). Then make it a material instance. And finally pick a random integer for that scalar parameter every time you spawn a ship.

hey, thanks for answering, your mat works well :slight_smile: thank you, but one thing, once the object is put into the editor, if i move the object it continuously changes the textures, anyway for it to stay one color once its spawn?

Hey there! Sorry for the delay, I didn’t saw your comment to my answer at first… There is indeed a way, go to your Blueprints Properties, and underneath the category “Blueprint Option”, the first checkbox is “Run Contruction Script on Drag”.

Disable it, and your construction script will run only once placed in the world :slight_smile:

hey, thanks for this but when I start doing blueprint like how you did it and after that once the object is put in to the editor its still not changing.
I have made the random texture array global and added different textures through detail panel but its still not working. Do I have to code inside the event graph of blueprint also?

Hey, I figured it out… by the way thanks for this its great…

How did you get this to work? I am also having problems and cannot figure out why the texture array does not function. Did you add an actor to the blueprint?

Ok, its late. I was able to get this to work. My mistake was the fact that my elemnt ID did not match to my material element ID. Once I corrected this it now works. The only issue I am having now is that the texture changes when the object is moved. Where would I find the blueprint options to disable “Run Contruction Script on Drag” ?

hello, The “Run Construction Script on drag” check box is now under the Class Settings button of your Blueprints, hope that helps

Found it. Thanks!

You can randomize the colors/textures just using one material, what you need is actually make a simple script using the node “per instance random”. Anyway I leave you here a two minutes tutorial where you can learn more about that, and you can see how the randomization work :wink:

I hope it could be helpful for you. Good luck with your project :wink:

you find a spawn pad