- Home /
Is there a way to call up a IF statement like a function?
In a script i wrote, i have it so that when the OnTriggerEnter function is called, it will run, do a IF statement to figure out what activated the Trigger, and then do the action it is expected to do depending on what triggered it. I have it set so that when the Trigger is activated it will spawn some enemies. I wanted to make this script universal so i made it so that if it was triggered, it would spawn however many enemies the user set it as, in the spots the user wanted it at.
var spawnobject : Transform; var spawnspot : Transform[]; var numberofspawnspots : int =3; private var maxspots : int = 0;
function OnTriggerEnter (other : Collider) { if (other.gameObject.tag == "Player" ) { var numberspots = maxspots; Instantiate(spawnobject, spawnspot[numberspots].transform.position, Quaternion.identity); if (numberspots < numberofspawnspots) { if (numberspots == numberofspawnspots--){ destroy(); } numberspots++; maxspots++; print(numberspots); return; } destroy(); } return; }
function destroy (){ Destroy(this, 0.0); }
Now that you see the code, it is easier to explain. I have it so that it will run through the code if the object is tagged "Player". I did this so when i shoot the bullet doesn't trigger it. Now the problem is, i have it so it returns after every check. Return sends it back to the top of the script so it has to start the process over again, which means i have to retriggger it, which isn't what i want. I want so it will just return to the IF statement. Is there anyway to do that?
Answer by Sebas · Feb 15, 2010 at 05:03 AM
It sounds like you want to spawn multiple enemies. The current script most likely only spawns a single one; that's why you want the if statement to occur multiple times. That sounds like a good spot for a loop, like Leigh suggested. Check out a while loop. It will cycle through your method while a certain condition is met. Your condition would probably be that the current number of enemies is less than your specified number of enemies to be spawned. You would have to restructure your if-statements and substitute for a while loop.
I simplified a little bit, because I'm unsure of your use of all variables, but this should give you a hint of how to solve your problem:
var numberofEnemiesToBeSpawned : int = 3; private var spawnedEnemies : int;
function OnTriggerEnter (other : Collider) { if (other.gameObject.tag == "Player" ) { while (spawnedEnemies < numberofEnemiesToBeSpawned) { //instantiate one enemy spawnedEnemies++; } } }
Thanks so much, i didn't even know of the existence of the while command. It seems to me it is going to be very useful in the future! Thanks again!
Answer by Leigh · Feb 15, 2010 at 04:43 AM
I'm a little unclear on exactly what you want to do.
OnTriggerEnter will occur once when the collision starts, and return will exit the method completely (ie. another collision event will have to occur before that method runs again.)
Are you wanting a loop to occur inside your method to spawn multiple GameObjects?
I want it so it will spawn all the objects at the same time, rather then having to enter and exit and reenter however many times. So in other words i want it so that it will spawn all the ones it needs to at one time, and without me adding a function for every single one, as i know how to do that, but it would be me adding 100+ functions for a simple thing like this.
Answer by Brian-Kehrer · Feb 15, 2010 at 05:01 AM
It's not very clear what you want, but it sounds like you want 'else' or 'switch'
if (x){
}
else if(y){
}
else{
}