Search Unity

[Solved] Array instantiates two objects instead of one.

Discussion in 'Scripting' started by SiMULOiD, Nov 27, 2015.

  1. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Hi,

    I'm using the below code to instantiate an object after the array is true.
    Problem is, I'm getting two instantiations instead of just one.
    Why is that?


    Code (CSharp):
    1.  
    2.  
    3. var OBJECT1 : GameObject;
    4. var allIn : boolean[]=new boolean[1];
    5. var i = 0;
    6. var minWait = 1.0;
    7. var maxWait = 10.0;
    8.  
    9. var prefabGameObject : GameObject;
    10.  
    11. function OnTriggerEnter (other: Collider){
    12.  
    13. if(other.GetComponent.<Collider>().gameObject.tag == "Thing1") allIn[0]=true;
    14.    
    15.     else return;
    16.  
    17.     // Check array is all false
    18.     var doIt=true;
    19.  
    20.     for(i=0; i<1; i++)
    21.         if(! allIn[i])
    22.             doIt=false;
    23.  
    24.     // Do something if true
    25.     if(doIt)
    26.         {
    27.          
    28.  
    29. yield WaitForSeconds(Random.Range(minWait, maxWait));
    30.                    
    31. {
    32.          
    33.               Instantiate(OBJECT1, transform.position, transform.rotation);
    34.            
    35. }          
    36.                    
    37.      
    38.  
    39.    /Destroy(this.transform.gameObject);
    40.  
    41.  
    42.    
    43.    
    44.         Debug.Log("GROWTH NEEDS MET");
    45.    
    46. }
    47. }
     
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    Is it possible that one of the colliding objects has multiple Collider components on it? OnTriggerEnter may be firing multiple times for a single trigger. Verify that your objects only have one collider on them (including any children objects).
     
  3. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Bingo!
    Thanks, kru!