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

Spawn issues

Discussion in 'Scripting' started by Frankzillaman, May 21, 2015.

  1. Frankzillaman

    Frankzillaman

    Joined:
    Mar 20, 2015
    Posts:
    22
    I have my trigger set to spawn my random prefab from array when entered.
    I am trying to get the prefab to spawn 10 units to the right each time the trigger is hit.
    Only problem i am getting is, it will work sometimes, other times it will go to a random position.
    Here is the code, can anyone tell me what is wrong? Thanks in advance..

    Code (CSharp):
    1. bool hasSpawned = false;
    2.     public GameObject[] myObjects;
    3.  
    4.  
    5.     void OnTriggerEnter(Collider col)
    6.     {
    7.                 if (col.tag == "Player") {
    8.                         if (!hasSpawned) {
    9.                                 hasSpawned = true;
    10.                                 Spawn ();
    11.                         }
    12.                 }
    13.         }
    14.     void Spawn()
    15.     {
    16.         Instantiate (myObjects [Random.Range (0, myObjects.Length)], transform.position + new Vector3(10,0,0), Quaternion.identity);
    17.  
    18.  
    19.     }
    20. }
    21.  
     
    Last edited: May 21, 2015
  2. Frankzillaman

    Frankzillaman

    Joined:
    Mar 20, 2015
    Posts:
    22
    updated the code a little , but still getting same issue.
    Code (CSharp):
    1. bool hasSpawned = false;
    2.     public GameObject[] myObjects;
    3.  
    4.  
    5.     void OnTriggerEnter(Collider col)
    6.     {
    7.                 if (col.tag == "Player") {
    8.                         if (!hasSpawned) {
    9.                                 hasSpawned = true;
    10.                                 Spawn ();
    11.                         }
    12.                 }
    13.         }
    14.     void Spawn()
    15.     {
    16.         Instantiate (myObjects [Random.Range (0, myObjects.Length)], new Vector3(0,0,-158.5f), Quaternion.identity);
    17.  
    18.  
    19.     }
    20. }
    21.  
     
  3. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451

    Untested, but see if this works better for you...

    Code (CSharp):
    1.     bool hasSpawned = false;
    2.     public GameObject[] myObjects;
    3.     private GameObject myObject;
    4.    
    5.     void OnTriggerEnter(Collider col)
    6.     {
    7.         if (col.tag == "Player")
    8.         {
    9.             if (!hasSpawned)
    10.             {
    11.                 hasSpawned = true;
    12.                 Spawn ();
    13.             }
    14.         }
    15.     }
    16.  
    17.     void Spawn()
    18.     {
    19.         myObject = myObjects[Random.Range (0, myObjects.Length)];
    20.         Instantiate(myObject, new Vector3(transform.position.x + 10, transform.position.y, transform.position.z), Quaternion.identity);
    21.     }