- Home /
Random.Range Spawner
I'm trying to make 1 of 3 monster to spawn randomly but this code spawns all of them at the same time.
var valo : GameObject;
var monster1 : GameObject;
var monster2 : GameObject;
var monster3 : GameObject;
function Start(){
Time.timeScale = 1;
yield WaitForSeconds(1);
particleEmitter.emit = false;
valo.active = false;
yield WaitForSeconds(1);
var chance =(Random.Range(1, 4));
if(chance == 1){
monster1.SetActiveRecursively(true);
}
else if(chance == 2){
monster2.SetActiveRecursively(true);
}
else if(chance == 3){
monster3.SetActiveRecursively(true);
}
}
Answer by whydoidoit · Jul 12, 2012 at 10:06 AM
That code isn't spawning anything (it is not doing an Instantiate) - I presume your 3 monsters are already in the scene (if they are all appearing then I would imagine that they are all active and that this code just sets them active again - which won't do much).
You could try this in addition:
function Awake() {
monster1.SetActiveRecursively(false);
monster2.SetActiveRecursively(false);
monster3.SetActiveRecursively(false);
}
Which would ensure that they were all deactivated.
EDIT The problem was that the script was attached to more than one object.
Yes they are already in the scene but not active. I have tried Debug.Log(chance) and it shows like "1 2 2 3 1 2 1 2 3 3 2 2 1..." and it probably makes all the monsters active? But I want it to stop at "1" or "2" or "3" so only one could spawn.
How is this being called multiple times is what I don't understand - if it's just attached to that one object it should only be called once - are you sure you don't have it attached to the monsters too or something?
Oh, It is attached to Campfires Smoke, InnerCore and OuterCore. I'll try removing it from 2 of these.
Your answer

Follow this Question
Related Questions
(C#) Help with spawning a random GameObject 1 Answer
How do i prevent an object instantiating another object straight away 2 Answers
Not repeating random 2 Answers
Using Vector3 in ViewportToWOrldPoint 1 Answer
How To Random Range a Float? 2 Answers