x


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?

more ▼

asked Feb 15 '10 at 04:29 AM

xToxicInferno gravatar image

xToxicInferno
485 24 28 41

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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++;
}
}
}
more ▼

answered Feb 15 '10 at 05:03 AM

Sebas gravatar image

Sebas
4.1k 12 18 45

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!

Feb 15 '10 at 05:12 AM xToxicInferno
(comments are locked)
10|3000 characters needed characters left

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?

more ▼

answered Feb 15 '10 at 04:43 AM

Leigh gravatar image

Leigh
59 2 2 7

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.

Feb 15 '10 at 05:00 AM xToxicInferno
(comments are locked)
10|3000 characters needed characters left

It's not very clear what you want, but it sounds like you want 'else' or 'switch'

if (x){
}
else if(y){
}
else{
}
more ▼

answered Feb 15 '10 at 05:01 AM

Brian Kehrer gravatar image

Brian Kehrer
2.8k 9 11 50

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5270
x1725
x304

asked: Feb 15 '10 at 04:29 AM

Seen: 950 times

Last Updated: Feb 15 '10 at 05:14 AM