Random the location of clone objects but retaining the same amount of clone objects.

Hi guys,

I want to randomized the location of my clone objects at different time but I want to retain the same amount of clone objects. What should i do? I want my clones to continuously changing their location. Here’s my coding.

void Start ()
    {
        StartCoroutine (waitSpawner());

    }

    void FixedUpdate ()
    {
        spawnWait = Random.Range (spawnLeastWait,spawnMostWait);

    }

    IEnumerator waitSpawner ()
    {   
       
        yield return new WaitForSeconds (startWait);

        while (!stop)
            {   
           
                randFood = Random.Range (0, foods.Length);

                countClones = Random.Range (0, clones.Length);

                Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 1, Random.Range (-spawnValues.z, spawnValues.z));

                clones [countClones] = Instantiate (foods [randFood], spawnPosition, gameObject.transform.rotation);

                i += 1;

                yield return new WaitForSeconds (spawnWait);
           
            }   
    }

Thanks!

Could you explain that, again? Here is the part that confused me a little.
Do you want to spawn objects, then move them randomly (not making more)
Do you want to spawn a # of objects, and move them randomly (possibly before all spawning is done, move the already spawned ones)?
Or something else I didn’t think of?

Note: There is no need to get a random number in fixed update constantly. Simply ask for that random number value before you use it (eg: in your yield return new WaitForSeconds). As it stands, you’re probably creating ~50 random values every second, but only using 1 every (whatever time frame). :slight_smile: