- Home /
Specific collision applying to all collisions in a script...
I have a script to change a variable when an object collides with an other certain object, but it is applying to all collisions that happen to it. Here is the code.
function OnCollisionEnter(collision : Collision){ if(collision.gameObject.tag == "MainChar" && Walking.runspeed > 8); Walking.runspeed = Walking.runspeed - 7; yield WaitForSeconds (waittime); Walking.runspeed = Walking.runspeed + 7; }
Answer by Owen-Reynolds · May 26, 2013 at 02:43 PM
Semi-colon after the if shouldn't be there. If you get a chance, this is the sort of thing any so-so intro programming book will cover. I've had people say the "Head First" books aren't bad. The rules for ifs are pretty much the same for all languages:
if( ... ) // <-- no semi-colon
{ // <- curlybrace marks start of if code
...
} // <- close curly marks end of if code
You think, "but wait, semicolons go at the end of all statements." The thing is, the IF isn't done yet. A semi-colon says "if this is true...nevermind." Then the stuff you meant to be in the if, becomes just normal "do all the time" code.
Likewise, the code inside needs curly-braces. When the computer sees "if(A) then B; C;" it can't tell whether C is part of the if, or "now the if is done, keep going." If you leave out the {}'s, the computer just guesses you meant to have them around only B.
Thanks, my main problem is gone, but now it doesn't slow at all.I think the problem is with the if conditions. I have used the exact name of the gameObject, so I don't know why it is happening though.
Try with only speed-7. I'm not sure you can use yield in an OnCollision. But even if you can, test out simple stuff first. Put in Debug.Log("slow");
to double-check (or whatever javascript uses.)