- Home /
random prefab instantiation
hello all, i have a question about random instantiation. i want to be able to randomly instantiate a random prefab and then after a short while, i want it to be destroyed. this is going to be utilized in a dynamic weather system do give you a better idea of what im wanting. im not really fluent in scripting yet so i was wondering if someone could help me with this. thanks!
Answer by Hashgrid · Jan 28, 2013 at 09:12 PM
Random.Range te permettra d'avoir l'aléaoire de l'instanciation:
http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html
Il te suffit de faire un if pour vérifier combien le Random t'a rendu.
Destroy te permettra de le détruire:
http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html
Je vais etre gentil et te donner un début de code, mais crois moi le mieux est d'aller sur les scriptings reference et de bien les lire. Par contre je code en c#, donc si t'es en JS je peux pas t'aider.
float y = 0;
y = Random.Range;
if(y >= 0 && y < 0.5)
{
Instantiate(PREFAB1, position, rotation);
}
else if(y >= 0.5f && y < 1)
{
Instantiate(PREFAB2, position, rotation);
}
Ensuite sur le prefab que tu instancie, tu rajoutes un script du genre (le 1 veut dire "au bout d'une seconde") :
Void Awake ()
{
Destroy(this, 1);
}
Au passage, je remarque que quand je tape < suivit de = dans un script, mon message se coupe mystérieusement, est-ce normal?
Answer by Eric5h5 · Jan 28, 2013 at 09:07 PM
var objects : GameObject[];
function Start () {
while (true) {
var delay = Random.Range (1.0, 10.0);
Destroy (Instantiate (objects[Random.Range (0, objects.Length)]), delay);
yield WaitForSeconds (delay);
}
}
Your answer
Follow this Question
Related Questions
How can instantiate in set random places? 1 Answer
one prefab into random position of grid 0 Answers
Randomized weather, little stuck 1 Answer
Randomly choosing sounds ? 2 Answers