- Home /
Spawning objects with random sizes and random seperation
I'm trying to spawn spheres that are randomly separated and have random sizes. Right now my script just spawns one sphere and I get this error
"InvalidCastException: Cannot cast from source type to destination type. Particle Script.Start () (at Assets/Particle Script.js:10)"
Here is my script
var numberofpartices= 10; var particles=transform; var seperation= Random.Range (-10,10); var particlemaxsize=5;
function Start () {
for (var i : int = 0;i < numberofpartices; i++) { var numRandom: int = (Random.Range(0,10)+.5); var particleMade: GameObject = Instantiate (particles, Vector3(i*seperation, 0, i*seperation), Quaternion.identity); particleMade.transform.localScale= Vector3.one *numRandom; }
}
how do I get it to spawn randomly sized and randomly spaced spheres? thanks for the help!
Answer by Mickydtron · Dec 21, 2010 at 06:39 PM
your second line is incorrect. instead of
var particles = transform;
you should have:
var particles : Transform;
then this object will have an exposed variable in the inspector called "particles". You will want to take your sphere prefab (you are using a prefab, right?), and place it in there. You can also change your Instantiate line to:
var particleMade = Instantiate(...);
Your answer
Follow this Question
Related Questions
Spawning enemies in waves 1 Answer
How do I create a spawn point 2 Answers
NullReferenceException help 1 Answer
InvalidCastException error and logic error 1 Answer
Spawn player if player dead 2 Answers