Search code examples
c#unity-game-engine2dspawn

Why does my obstacle spawn not follow the same spawning pattern?


Im making a game where you have 3 blocks to move within and theres obstacle in two of them, heres a picture picture of game. the problem im having is that the obstacles sometimes take a break of like 3 seconds and then spawns again and then wait for sometime and start spawnining again. the pattern is not continous. I want them to always spawn with like a second inbetween.

  using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

public class merHinder : MonoBehaviour
{
    public Rigidbody2D orginal;
    private Vector3[] spawnPos = new Vector3[3];


    // Start is called before the first frame update
    void Start()
    {
        //InvokeRepeating("timer", 0f, 1f);
        StartCoroutine(SpawnAtIntervals(1f));
    }

    // Update is called once per frame
    void Update()
    {


        spawnPos[0] = new Vector3(3, 3.86f, -5);
        spawnPos[1] = new Vector3(-3, 3.86f, -5);
        spawnPos[2] = new Vector3(0, 3.86f, -5);




    }
    private void FixedUpdate()
    {

    }
    void Spawn()
    {
        int randomNumber = Random.Range(0, spawnPos.Length);
        int randomNumber1 = Random.Range(0, spawnPos.Length);
        if (randomNumber != randomNumber1)
        {
            Rigidbody2D clone1 = Instantiate(orginal, spawnPos[randomNumber], Quaternion.identity);
            Rigidbody2D clone2 = Instantiate(orginal, spawnPos[randomNumber1], Quaternion.identity);
        }
        //Destroy(orginal);



    }
    bool keepSpawning = true;

    IEnumerator SpawnAtIntervals(float secondsBetweenSpawns)
    {
        // Repeat until keepSpawning == false or this GameObject is disabled/destroyed.
        while (keepSpawning)
        {
            // Put this coroutine to sleep until the next spawn time.
            yield return new WaitForSeconds(secondsBetweenSpawns);

            // Now it's time to spawn again.
            Spawn();
        }
    }
}

Solution

  • They only spawn if the random numbers are different. But sometiomes random results in the same number.

        int randomNumber = Random.Range(0, spawnPos.Length);
        int randomNumber1 = Random.Range(0, spawnPos.Length);
        if (randomNumber != randomNumber1) // <- No spawn if both random numbers are the same
        {
            Rigidbody2D clone1 = Instantiate(orginal, spawnPos[randomNumber], Quaternion.identity);
            Rigidbody2D clone2 = Instantiate(orginal, spawnPos[randomNumber1], Quaternion.identity);
        }
    

    Possible solution (bit hacky, but no heap allocation):

        void Spawn()
            {
                int randomNumber = Random.Range(0, spawnPos.Length);
                int randomNumber1 = Random.Range(0, spawnPos.Length);
                while (randomNumber == randomNumber1)
                {
                    // try again until numbers are different
                    randomNumber1 = Random.Range(0, spawnPos.Length);
                }
                Rigidbody2D clone1 = Instantiate(orginal, spawnPos[randomNumber], Quaternion.identity);
                Rigidbody2D clone2 = Instantiate(orginal, spawnPos[randomNumber1], Quaternion.identity);
            }