3
\$\begingroup\$

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)?

touchcube!

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.

Inspector:

inspector

And the code:

code

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

enter image description here

\$\endgroup\$
4
+50
\$\begingroup\$

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) {
     Application.LoadLevel("Level1");
}

That should do ya!

\$\endgroup\$
2
\$\begingroup\$

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

void OnCollisionEnter (Collision col)
{
    Application.LoadLevel("Level1");
}

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

void OnTriggerEnter (Collider other)
{
    Application.LoadLevel("Level1");
}

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'.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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: https://unity3d.com/learn/tutorials/modules/beginner/physics/colliders-as-triggers

\$\endgroup\$
  • \$\begingroup\$ Is this in JavaScript? \$\endgroup\$ – kprovost7314 May 3 '15 at 4:55
  • \$\begingroup\$ Do I create that function then put Application.LoadLevel() in it? \$\endgroup\$ – kprovost7314 May 3 '15 at 5:06
  • \$\begingroup\$ It is c#. And yes, you put Application.LoadLevel() inside the function \$\endgroup\$ – Cafn May 3 '15 at 19:00
  • \$\begingroup\$ I enabled "is trigger" on the collider settings (for the cube that's supposed to take you to the next level) with Application.LoadLevel() and all it does is go right through the cube without leaving the scene. \$\endgroup\$ – kprovost7314 May 3 '15 at 23:08
  • \$\begingroup\$ That Cube has the script added to itself? Post some screenshots of code and inspector \$\endgroup\$ – Cafn May 4 '15 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.