Assigning random color to Blender model submeshes at instantiation

Hi guys,

I am making a game that involves a prefab to create enemies (monsters).

I created the model in Blender and exported it as .fbx thus I was able to import it into Unity.

Now the structure is the following.

Empty game object (Enemy) → Monster (contains the entire model) → Body / Legs / Arms;

Now the body part of the model consists of three submeshes. Each mesh contains a set of colors for arms, legs, torso for example. I can change these colors directly over the inspector.

Now the goal is to create these monsters with random colors when they are instantiated. I imagine the easisest way to do this would be to attach a script to the bodymesh for example and somehow access the “arm” component and assign it a random color.

Is this the right way to do it?

Thanks

Submeshes use a material array, so you’d get the array from renderer.materials, make changes, and assign the array back.

–Eric

What Eric suggests is the way to go, and you could easily traverse the hierarchy of game objects and find things with mesh renderers and then dig up their materials and change their colors on the fly.

But you might find a more-practical way in Unity is to create a specific setup script for the monster, then create a prefab with your FBX and that script at the root of your hierarchy of objects, and it would have a series of links in it to give you finer-grain control over the colorization behavior you want.

Think of the script as sort of a nice adaptor that other things in your game can use to interact with the object. Perhaps it has a “You are poisoned!” function, and when called, whatever the monster color is, it gets tinted a bit green until the poisoned status wears off, at which point it would revert to its base color.