Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2D bug with a spawner script

Discussion in '2D' started by Raclette, Oct 22, 2015.

  1. Raclette

    Raclette

    Joined:
    Oct 16, 2012
    Posts:
    15
    Hello everyone,

    I made a spawner script that instantiate different elements that pass from the right to the left of the screen.
    But sometime the spawner instantiate the element in the middle of the screen and I don’t understand why.
    Can you help me?

    This in the spawner script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class Spawner : MonoBehaviour {
    6.  
    7.     public GameObject[] obj;
    8.     private float startWait;
    9.     private int objectCount;
    10.     private float spawnWait;
    11.     private float wavesWait;
    12.     public int stage;
    13.     private int objectPlus;
    14.  
    15.     void Start ()
    16.     {
    17.         wavesWait = 2;
    18.         stage = 0;
    19.         objectPlus = 0;
    20.         startWait = 2;
    21.         StartCoroutine(Spawn());
    22.  
    23.     }
    24.  
    25.     IEnumerator Spawn ()
    26.     {
    27.  
    28.         yield return new WaitForSeconds (startWait);
    29.  
    30.         while (true)
    31.         {
    32.             for (int i = 0; i < objectCount; i++)
    33.             {
    34.  
    35.                 Instantiate(obj[Random.Range (0,obj.Length)], transform.position, Quaternion.identity);
    36.                 yield return new WaitForSeconds (spawnWait);
    37.  
    38.             }
    39.  
    40.             stage ++;
    41.             yield return new WaitForSeconds (wavesWait);
    42.         }
    43.  
    44.     }
    45.  
    46.     void Update ()
    47.     {
    48.  
    49.         if (stage == 0) {
    50.  
    51.             objectCount = 3 + objectPlus;
    52.             spawnWait = 2f;
    53.  
    54.         } else if (stage == 1) {
    55.  
    56.             objectCount = 3 + objectPlus;
    57.             spawnWait = 1f;
    58.  
    59.         } else if (stage == 2) {
    60.  
    61.             objectCount = 3 + objectPlus;
    62.             spawnWait = 0.8f;
    63.            
    64.         } else if (stage == 3) {
    65.  
    66.             objectCount = 2 + objectPlus;
    67.             spawnWait = 0.4f;
    68.            
    69.         } else if (stage == 4) {
    70.  
    71.             objectCount = 3 + objectPlus;
    72.             spawnWait = 0.6f;
    73.            
    74.         } else if (stage == 5) {
    75.  
    76.             objectCount = 3 + objectPlus;
    77.             spawnWait = 0.4f;
    78.  
    79.         } else if (stage > 5) {
    80.             stage = 0;
    81.             objectPlus ++;
    82.         }
    83.  
    84.     }
    85.  
    86. }
    87.  
    88.  
    89.    
    90.  
    And that is the code that are on each object spawned
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ObstacleScript : MonoBehaviour {
    5.  
    6.     private static float obstacleSpeed;
    7.  
    8.     void Awake ()
    9.     {
    10.         obstacleSpeed = 10;
    11.     }
    12.  
    13.     void Update ()
    14.     {
    15.  
    16.         GetComponent<Rigidbody2D> ().velocity = new Vector2 (-obstacleSpeed,0);
    17.  
    18.         if (transform.position.x < -10)
    19.         {
    20.  
    21.             Destroy (gameObject);
    22.         }
    23.  
    24.         if (transform.position.x == 0 && transform.position.y == 0)
    25.         {
    26.  
    27.             Destroy (gameObject);
    28.             Debug.Log("Bug");
    29.  
    30.         }
    31.    
    32.     }
    33.  
    34. }
    Thank you very much.
     
  2. LiberLogic969

    LiberLogic969

    Joined:
    Jun 29, 2014
    Posts:
    138
    Doesn't look like the problem is in any of the scripts you posted... it could have something to do with how your scene is setup...

    Where is the GameObject with the Spawner script positioned in your scene? When you create the instance of your obstacle in the Spawner class you are passing in the Spawner GameObjects position, make sure that's not changing at anypoint...
     
  3. Raclette

    Raclette

    Joined:
    Oct 16, 2012
    Posts:
    15
    The Spawner GameObjects are not changing. But I think I resolved the issue but I don’t know why that was the problem.
    On my prefabs that are instantiate by the Spawner GameObject, the Freeze Position Y was enable. I don’t know why but it seams to be the problem.