I have a long OnCollisionEnter script with a lot of if statements in it, and I would like the script to stop itself at various points if certain conditions are true. I’ve tried the command “StopAllCoroutines()” and it doesn’t appear to do anything. Is there any other way to stop the script (without disabling it)?
Something like this, maybe? (The return will exit the function)
function OnCollisionEnter() {
// some code here
if( some condition ) {
return; // nothing more to do, so return from the function
}
// some more code
}
If you want to switch off everything if some condition is set, you could just put that if/return at the start of the function:
var disableCollisionEnter = false;
function OnCollisionEnter() {
if( disableCollisionEnter ) {
return; // nothing to do, so return from the function
}
// some code
}
I tried this but it doesn’t seem stop the whole script. What I need to do is stop the whole script from an “if” command within the script. If this doesn’t make sense I can give you an example.
For example in the code below, if condition2 is met, then DoSomething2, then stop the script, do not even check if condition3 is met. (I do not want to disable or deactivate anything). Or is there a way to skip over parts of a script, and jump to the end? The return stops the script when it is not under the “if” command. Unfortunatly I need it to be in the “if” command.
function OnCollisionEnter () {
if (condition1) {
DoSomething1;
return;
}
if (condition2) {
DoSomething2;
return;
}
if (condition3) {
DoSomething3;
return;
}
}
I recommend you check out the fpsplayer and AIscripts in the fps demo, because it seems to me you can create whatever function you want, and reference to parts it dependent on certain conditions…all in single scripts, not inside function OnCollisionEnter mind you…but hth
AC
The code you entered above should exit the function inside the if. Note, when you write “stop the script”, you make it sound like you want to disable the entire script, although a single script can contain more than one function. I assume that you just want to exit OnCollisionEnter.
Also not that OnCollisionEnter can be called more than once. Are you sure that the “condition 3” part is not being called on a different iteration?
You could also use the “else” part of the if statement. The following should be equivalent to your example:
function OnCollisionEnter () {
if (condition1) {
DoSomething1;
}
else if (condition2) {
DoSomething2;
}
else if (condition3) {
DoSomething3;
}
}
Thanks everyone. It seems to be working now. This is for a bowling game I’m working on. It is posted in the showcase forum (incomplete version). I will have an improved version posted soon with scoring.