- Home /
help with spawning objects
unity always freeze when I prees play. I think there is a problem with for loop but i cant figure it out. heres the code im using. var ball: GameObject;
var ball: GameObject;
function Start() {
var ballNum = 3;
for (var i = 0; i <= ballNum; i++){
var position = Vector3(Random.Range(1, 10), 0, 0);
Instantiate(ball, position, Quaternion.identity);
}
}
looks like the script should create 4 balls with no problems. Can you post more of your script?
what do you mean by Unity freeze? Do you mean you have no option but to force quit Unity?
yes. it completely stopped working so i had to use ctrl+ alt+del to force quit it. but now it is working when i press play, it just keep spawning balls.
Answer by highpockets · Nov 23, 2013 at 07:15 PM
for (var i : int = 0; i
you didn't declare i correctly, change the for line to what I show above
it still doesnt working.Now it keeps spawning balls and dont stop. but i find out that if I press pause button and then play button it works fine.
You sure this script isn't showing the function Update ( ) and not the function Start ( ). It doesn't make sense that it would run through the for loop more than 4 times and therefore keep instantiating more balls. If it keeps instantiating balls even after your game starts, then there must be something happening elsewhere
Now I'm just totally guessing, but do you maybe have ball set to itself or another object which has the above script?
You'll then have a recursive behavior where each object will create 4 objects, which will each create 4 objects, etc...
ya, you should put this script on an empty game object if it is not already
Answer by Programmer-Tau · Nov 23, 2013 at 07:06 PM
I don't see anything wrong with the loop itself. Are you assigning something to ball?
Seems like either spamming Debug.Log() or putting break points should give you the answer quickly.
yes, I have assign object to ball. i just tried with Debug.Log and it works fine.. it logs string 4 times, so I guess there is nothing wrong with loop.
Your answer
Follow this Question
Related Questions
Spawning Objects 1 Answer
Random loot - rarity 3 Answers
error CS1502 and CS1503 1 Answer
Spawning A Grid Of Cubes 2 Answers
How to make the clones of a zombie continue to clone? 2 Answers