Loading a Level on Collision?

For some reason, it’s not working. Help?

function OnCollisionEnter(collision : Collision){
  if(collision.gameObject.tag == "Player"){
    Application.LoadLevel(2);}}

I have my Build Settings right, and I double checked my tags. Any ideas?

Are you sure OnCollisionEnter is even being called? If so, how do you know?

Why wouldn’t it be? I don’t understand programming at all…

Take it from the top.

Do objects have colliders to create the collision? We know collisions have a gameObject, and a GameObject has a tag. Have you properly set the “Player” tag in the editor?

If those are not right, fall back to a Debug.Log even or comparable in the OnCollisionEnter.

if OnCollision giving you troubles, you may use OnTriggerEnter… have you tried ?

greetings.

Callbacks such as OnCollisionEnter() are only called when certain conditions are met (e.g. when two objects run into each other). If for some reason those conditions aren’t met (e.g. the game objects aren’t configured properly), the function won’t be called.

Furthermore, there’s at least two other potential failure points in your code (which were mentioned previously). First, the colliding game object may not have a tag of “Player” (maybe you forgot to assign the tag, or the capitalization is wrong, or something like that). Second, Application.LoadLevel() may be failing for some reason or other (e.g. because there is no level in the build settings with an index of 2).

By using the debugger and/or debug output, you should be able to narrow things down significantly. For example, by placing print() statements at the very beginning of the function and after the first conditional, you can most likely determine where the failure point is exactly.

Like I said, I checked all these things.

Replace the whole thing with this:

function OnCollisionEnter(collision : Collision){
  print(collision.collider);
}

Repeat the steps it would take to hit it, if nothing goes into your Console, the hit is not happening.

MAKE SURE your player is NOT on IGNORE RAYCAST else it wont work at all.

Well, you said:

It’s not really clear from that that you’d already checked the things we mentioned. For example, ‘checked the tags’ could just mean checking in the inspector to see what tags the objects in your scene have, but what we’re talking about is (e.g.) stepping through the code in the debugger, or adding debug output to determine exactly what is and isn’t occurring. Also, you didn’t say anything about checking to see if the function was being called in the first place.

Anyway, for something like this, you have to be thorough and methodical. If you have tried all the things that’ve been mentioned, perhaps you could clarify that, and also tell us what the results were. Otherwise, as mentioned, you should use the debugger, debug output, or the process of elimination to determine exactly where the failure is occurring. In other words, you should be able to tell us something like, ‘The function is never being called,’ or ‘LoadLevel() is being called, but nothing is happening’.

Well assuming it’s not being called, how would I fix it? I don’t understand why if my code is correct why it’s not working.

Is that just an assumption, or do you know for a fact that it’s not being called?

If it’s just an assumption, then the first step will be to determine conclusively whether or not the function is being called. Here are two ways you can do this:

  1. Use the debugger, place a breakpoint on the first statement in the function body, enter play mode, and see if the breakpoint is tripped at the appropriate time. (I’m assuming the MonoDevelop debugger works with UnityScript, although I’ve never tried it.)

  2. Add a print() statement (as the first line of code in the function) that prints a unique, easy-to-identify message. Enter play mode, and see if the message appears in the console at the appropriate time.