Search Unity

Instantiating 3 different types of objects at random?

Discussion in 'Scripting' started by DarkNeo, Feb 14, 2015.

  1. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Hey there,

    Can anyone please help me, My following code will only instantiate the building1 and repeat it over and over again, that is perfect but I would like the Instantiate to randomly pick 1 of the 3 buildings that I have in my scene and load that and so on and so forth.

    Code (JavaScript):
    1.  
    2. var building1 : GameObject;
    3. var building2 : GameObject; // 2nd Building that I would like to also Instantiate
    4. var building3 : GameObject; // 3rd Building that I would like to also Instantiate
    5.  
    6.  
    7. private var CreateTimer = 2;
    8. private var NextCreate = 0.0;
    9. private var count = 0;
    10.  
    11. function Update()
    12. {
    13.   if(Time.time > CreateTimer + NextCreate)
    14.   {
    15.     NextCreate = Time.time;
    16.     for (var i : int = 0;i < 1; i++)
    17.     {
    18.       count++;
    19.    
    20.            Instantiate (building1, Vector3 (count * 256, 252, 10), Quaternion.identity);
    21.        
    22.     }
    23.   }
    24. }
    I hope someone can point me in the right direction thanks!
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    892
    Not really tested, but you could do something like this:

    Code (CSharp):
    1. var building1 : GameObject;
    2. var building2 : GameObject; // 2nd Building that I would like to also Instantiate
    3. var building3 : GameObject; // 3rd Building that I would like to also Instantiate
    4.  
    5. var buildings = new Array();
    6. private var CreateTimer = 2;
    7. private var NextCreate = 0.0;
    8. private var count = 0;
    9. function Start()
    10. {
    11.     buildings.push(building1);
    12.     buildings.push(building2);
    13.     buildings.push(building3);
    14. }
    15. function Update()
    16. {
    17.   if(Time.time > CreateTimer + NextCreate)
    18.   {
    19.     NextCreate = Time.time;
    20.     for (var i : int = 0;i < 1; i++)
    21.     {
    22.       count++;
    23.  
    24.            Instantiate (buildings[Random.Range[0,buildings.length], Vector3 (count * 256, 252, 10), Quaternion.identity);
    25.      
    26.     }
    27.   }
    28. }
    29.  
     
    DarkNeo likes this.
  3. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Cool Thanks I will have a look at it today :)

     
  4. ReeVee

    ReeVee

    Joined:
    Feb 3, 2014
    Posts:
    5
    This might work better will allow any number of random buildings. Also removed the for loop as it is not doing anything.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var buildings : GameObject[];
    4. private var CreateTimer = 2;
    5. private var NextCreate = 0.0;
    6. private var count = 0;
    7. function Update()
    8. {
    9.   if(Time.time > CreateTimer + NextCreate)
    10.   {
    11.       NextCreate = Time.time;
    12.       Instantiate (buildings[Random.Range(0,buildings.Length)], Vector3 (count * 256, 252, 10), Quaternion.identity);
    13.       count++;
    14.   }
    15. }
     
    DarkNeo likes this.
  5. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    That's cool thank you guys for the help it worked good and I understand how it works now. :) Cheers!
     
unityunity