- Home /
Random Spawning Question
I've been working on a random spawning script for power ups in a side scrolling shooter game but for some reason the spawner always fires it at the same time and not randomly, may I ask if anyone can see a problem with this script?
//What power up are we spawning? var PowerUp: GameObject; //Minimum alloted time var minTime: float = 1.0; //Maximum allotted time var maxTime: float = 10.0;
// function Update () { //Is the item already Out? if (GameObject.FindWithTag("PowerUp") == false){ //Wait for random time before respawning WaitForSeconds(Random.Range(minTime, maxTime)); //Set random position inside X&Y axis and materialize var position = Vector2(Random.Range(0.01, 0.5), Random.Range(0.10, 0.90)); Instantiate(PowerUp, position, Quaternion.identity); }
}
Answer by Mike 3 · Jul 31, 2010 at 02:28 PM
You need to use yield when you're using WaitForSeconds, and you can't do it from Update - Update can't be a coroutine. On top of that, you'd need to add a lock to prevent the coroutine being called multiple times and spawning loads of them.
Also, use == null instead of == false for your object checks - even if false works, it just looks odd
//What power up are we spawning? var PowerUp: GameObject; //Minimum alloted time var minTime: float = 1.0; //Maximum allotted time var maxTime: float = 10.0;
var spawning = false;
function Update () { //Is the item already Out? if (!spawning && GameObject.FindWithTag("PowerUp") == null){ SpawnObject(); } }
function SpawnObject() { spawning = true;
//Wait for random time before respawning
yield WaitForSeconds(Random.Range(minTime, maxTime));
//Set random position inside X&Y axis and materialize
var position = Vector2(Random.Range(0.01, 0.5), Random.Range(0.10, 0.90));
Instantiate(PowerUp, position, Quaternion.identity);
spawning = false;
}
@$$anonymous$$ike why do you always have to one-up me? I was here first... Just kidding. ;)
@$$anonymous$$ike - lol.... I hate this 15 character limit. I can't just write 'lol'.
Answer by e-bonneville · Jul 31, 2010 at 02:24 PM
Your script is fine, as far as I can see. I might have a solution, though. I don't know if this is your problem, but it is something that happens a lot with me. If you create a variable within your script, and then change it later on, it remains the same wherever the script is instantiated. For example, if you created minTime and maxTime as the same value, and then changed the value in the script, it would remain the same in the inspector. So just check the inspector and see if both time values are correct.
Also, I just noticed this on re-reading your script, you'll want to use yield WaitForSeconds
instead of just WaitForSeconds
. One more thing - it's always a good idea to space your code correctly. Instead of having
function Update() {
if (!isGoodFormatting) {
print ("This is not good formatting.");
}
}
You should have
function Update() {
if (isGoodFormatting) {
print("This is good formatting");
}
}
Your answer
Follow this Question
Related Questions
Reset GUI texture after respawn 1 Answer
Random Player Respawn Points 3 Answers
Respawn Code Null Reference Exception 1 Answer
Checkpoint system??? 3 Answers
die then respawn after couple of secs when touching a ridgid body? 1 Answer