Search Unity

Obstacle Holders don't keep spawning

Discussion in 'Scripting' started by dyach3579, Jun 29, 2019.

  1. dyach3579

    dyach3579

    Joined:
    Jan 3, 2018
    Posts:
    22
    Hey everyone, so I'm creating a 2D Endless Runner. I have my Spawner script as well as Spawner points that i placed in the scene. My obstacles spawn and move on the X axis, to the left.

    I have my array of obstacles and i drag my obstacle prefabs into the element slots in the inspector. This script works great for spawning individual obstacles. But if i add an Obstacle Holder prefab that i created that only has around 10 obstacles in each holder, it spawns the Holder and then it stops spawning everything all together. It won't even spawn the individual obstacles. I of course need the obstacles to keep spawning, but it stops for some reason after the Holder spawns.

    Any idea why it doesn't keep spawning the obstacles? Do i need to change something in the code? Heres my code Im using. I am new to coding, and Unity, so i appreciate any help.
    Code (CSharp):
    1. public class Spawner : MonoBehaviour
    2. {
    3.     [SerializeField] float minSpawnTime, maxSpawnTime;
    4.  
    5.     [SerializeField] GameObject[ ] obstacles;
    6.     [SerializeField] Transform obstaclesPos;
    7.  
    8.     Transform[ ] lastPoss;
    9.     float[ ] lastSpeed;
    10.  
    11.     private void Awake()
    12.     {
    13.         lastPoss = new Transform[obstaclesPos.childCount];
    14.         lastSpeed = new float[obstaclesPos.childCount];
    15.  
    16.         for (int i = 0; i < lastSpeed.Length; i++)
    17.         {
    18.             lastSpeed[i] = -1;
    19.         }
    20.     }
    21.  
    22.     void Start()
    23.     {
    24.         // StartObstaclesMove();
    25.     }
    26.  
    27.     public void StartObstaclesMove()
    28.     {
    29.         StartCoroutine(InstanteObstacels(0f));
    30.     }
    31.  
    32.     IEnumerator InstanteObstacels(float dt)
    33.     {
    34.         float waitTime = 1f;
    35.         yield return new WaitForSeconds(waitTime);
    36.  
    37.         if (GameManager.instance.bGameStarted)
    38.         {
    39.             int rr = Random.Range(0, obstaclesPos.childCount);
    40.  
    41.             Transform tt = obstaclesPos.GetChild(rr);
    42.             int randomObstacle = Random.Range(0, obstacles.Length);
    43.  
    44.             GameObject obs = GameObject.Instantiate(obstacles[randomObstacle]);
    45.             obs.transform.position = tt.position;
    46.  
    47.             waitTime = Random.Range(minSpawnTime, maxSpawnTime);
    48. 
            yield return new WaitForSeconds(waitTime);
    49.  
    50.  
    51.     if (canGo(rr, obs.transform.position.x, obs.GetComponent<ObstacleMovement>().moveSpeed))
    52.             {
    53.                 lastPoss[rr] = obs.transform;
    54.                 lastSpeed[rr] = obs.GetComponent<ObstacleMovement>().moveSpeed;
    55.             }
    56.             else
    57.             {
    58.                 Destroy(obs);
    59.             }
    60.  
    61.             StartCoroutine(InstanteObstacels(dt));
    62.         }
    63.     }
    64.  
    65.     bool canGo(int rr, float x1, float s1)
    66.     {
    67.  
    68.         if (lastPoss[rr] == null || lastSpeed[rr] < 0) return true;
    69.  
    70.         float x = lastPoss[rr].position.x;
    71.         float s = lastSpeed[rr];
    72.  
    73.         float t1, t2;
    74.  
    75.         t1 = (x + 10) / s;
    76.         t2 = (x1 + 10) / s1;
    77.  
    78.         if (t2 > t1) return true;
    79.  
    80.         return false;
    81.     }
    82. }