Getting the same random value for all generated prefabs

Hello everyone!

I’m currently setting up a scene to randomly create characters. And it works fine, textures and blend shapes generate randomly. However, for the ear prefab, I’d like to make the ears reference the same mesh. Random ears might be a bit much. Also, sometimes the ears don’t pick up a mesh at all and I get only one ear (I’ve double checked the file names, that doesn’t seem to be it).

I’d like to keep the random size of the ears, but make sure that the gameobjects always reference the same random mesh.

Here’s a picture to demonstrate my issue:

Here’s the script I use:

public class randomizer : MonoBehaviour {

    public string File_name;
    public int count;
    public bool sizeRandom;
    public float size;
 

    private void Start()
    {
        int id = Random.Range(0, count) + 1;
        MeshFilter filter = gameObject.GetComponent<MeshFilter>();
        CapsuleCollider collider = gameObject.GetComponent<CapsuleCollider>();

        filter.mesh = Resources.Load<Mesh>(File_name +"0"+ id) as Mesh;
        Bounds msh_bounds = filter.mesh.bounds;
        collider.radius = (msh_bounds.size.x + msh_bounds.size.z) * 0.25f;
        collider.height = msh_bounds.size.y;

     
        if (sizeRandom == true)
        {
            float newsize = Random.Range((size - size * 0.5f), (size + size * 0.5f));
            gameObject.transform.localScale = new Vector3 (newsize, newsize, newsize);

        }

    }

}

This is only my second project using C#, but I’m sure there must be a way of using an if statement or something similar in this case? I’d appreciate any ideas! :slight_smile:

thanks in advance

Well, this feels stupid, answering myself only minutes later. But in case someone else needs a similar solution:

I simply moved the part where the “id” is being set to be in a void Awake instead of in the void Start, like this:

 private void Awake()
    {
        id = Random.Range(0, count) + 1;
    }

God!! that’s so random :smile::smile: