- Home /
The question is answered, right answer was accepted
Select randomly x arrays and instantiate them
I created an array with 135 GameObjects, now, from that 135 GameObjects and from that 135 I want to randomly select 85 and spawn them, but how do I make to randomly choose 85 and spawn them |!!!I don't want to spawn 2 times the same GameObject!!!| <-- this is for real, I really don't want to have repeated GameObjects because the GameObjects are the same the only thing that changes is the position. If you know how to do it please comment my script edited by you if you don't mind. thanks in advance. #pragma strict
 var bricks : GameObject [];
 
 function Awake () 
 {
     GameObject.Instantiate(bricks[Random.Range(0,bricks.Length)], transform.position, transform.rotation);
 }
 
 function Update () {
 
 }
Easiest way would be to create and then store all your gameObjects in a List, and then simply use RemoveAt(index)
http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?
scroll down to Generic List
thanks must try that. edit: I have no ideia on what to do
Answer by AlucardJay · Mar 24, 2013 at 02:43 PM
List seems difficult to use, but really it is easy and very flexible once you do some practicing with it. Please read my script carefully, follow the comments, cross-reference with the link I provided, and see if you do understand what is going on :
 #pragma strict
 
 // to use List in uJS
 import System.Collections.Generic;
 // --
 
 
 var bricksToSpawn : int = 85;
 
 var bricks : GameObject [];
 
 var bricksList : List.< GameObject >; // declare a new list
 
 
 function Start() 
 {
     PopulateBricksList();
     
     ChooseRandomBricks();
 }
 
 
 function PopulateBricksList() 
 {
     // declare a new list
     bricksList = new List.< GameObject >();
     
     // get the length of the built-in array
     var totalBricks : int = bricks.Length;
     
     // add each brick to the brickList
     for ( var i : int = 0; i < totalBricks; i ++ )
     {
         bricksList.Add( bricks[i] );
     }
 }
 
 
 function ChooseRandomBricks() 
 {
     // choose a brick from the bricksList
     for ( var i : int = 0; i < bricksToSpawn; i ++ )
     {
         // find the current length of the bricksList
         var bricksRemaining : int = bricksList.Count;
         
         // get a random number
         var rndChoice : int = Random.Range( 0, bricksRemaining - 1 );
         
         // instantiate that chosen brick
         var cloneBrick : GameObject = Instantiate( bricksList[ rndChoice ], transform.position, transform.rotation );
         
         // remove that brick from the list
         bricksList.RemoveAt( rndChoice );
     }
 }
That link again : http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?
scroll down to Generic List
Ok it definitly works, I'm gonna study a little bit and then I will record an update video about my game and obviously I will refer you in description
Excellent ! Don't forget to accept any answer you are happy with =]
Here is my notepad of List notes, the things that get used the most :
 // to use List
 import System.Collections.Generic;
 
 // declare
 var inRangeList : List.< GameObject > = new List.< GameObject >();
 
 var inRangeList : List.< GameObject >;
 inRangeList = new List.< GameObject >();
 
 // Add object to list :  
 inRangeList.Add( theItem );
 
 // length
 inRangeList.Count;
 
 // read from position 
 inRangeList[ index ];
 
 // empty list
 inRangeList.Clear();
 
 // send to built-in array
 inRangeList.ToArray();
ok, if you are curious: http://www.youtube.com/watch?v=rDg-H$$anonymous$$I0F38&feature=youtu.be
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                