- Home /
Question by
SYRSA · Feb 28, 2014 at 06:00 AM ·
instantiateprefabrandom
Instantiating a random prefab from array.
I have no clue how to implement random range in this kind of function. Can someone clear things up so I don't confuse myself too much?
Here is the script that I'm having issues with:
var hitParticle : GameObject[];
function OnCollisionEnter(collision : Collision) {
for (var contact : ContactPoint in collision.contacts) {
var Hit = Instantiate (hitParticle, contact.point, Quaternion.identity);
}
}
Comment
Answer by robertbu · Feb 28, 2014 at 06:11 AM
Line 5 could be:
var Hit = Instantiate (hitParticle[Random.Range(0, hitParticle.Length)], contact.point, Quaternion.identity);
Note if you fail to initialize the hitParticle array, this code will generate an out of bounds error.
Thanks, I thought it would be written differently, but it wasn't.
Your answer
Follow this Question
Related Questions
Can't set the random position for an Instance. 1 Answer
instantiating vertically 2 Answers
Random.value sometimes doesn't instantiate my prefab(pic included) 1 Answer
How can I align instantiated prefabs randomly on runtime #C 0 Answers
instantiate random prefabs based on player camera distance 1 Answer