How to enter a level when touching an object

How to enter a level when touching an object

I am working on a project in Unity 4.6.1f1 and I was wondering how can I make a cube touch another cube to go to the next level (the image below)?
EDIT: I made a script and put it on the purple cube then enabled "is trigger" on the purple cube, but the blue cube goes right through it.

And the code:

EDIT 5/15/15: I edited my script and I was wondering if I'm doing something wrong.


Answer 1:

Your LevelCollider is attached to a Collider that is a trigger. That means it will not call OnCollisionEnter. It will call OnTriggerEnter.

Try adding this to LevelCollider:

void OnTriggerEnter(Collider other) {

That should do ya!

Answer 2:

Detect collisions with OnCollisionEnter then load your level there. I.e.:

void OnCollisionEnter (Collision col)

If your character’s Collider is a trigger, then you need to use OnTriggerEnter. I.e.:

void OnTriggerEnter (Collider other)

If neither of these work, try checking these things:

  • The character and object are set to collide in the layer collision matrix.

  • The scene called “Level1” is
    added to the build. You can check if it is by pressing Ctrl-Shift-B
    under ‘Scenes in build’.

Answer 3:

The command to load a level is Application.LoadLevel(). Call that from an “On Collision” function on the cube.

Answer 4:

You can add a box collider (Add -> Physics -> Box Collider) in the inspector.

Then, add a script with a function

 OnCollisionEnter(Collision other)

then add that script to the cube you want to collide with.

More info in here: