- Home /
Closed - Go to next scene on collision
I've looked at every tutorial, answer on here, and even in the stars but for the life of me, I can't get this to work.
2D Platformer
I'm trying to go to the next scene when the player enters the Cube I have set up.
Here's the code:
using UnityEngine;
using System.Collections;
public class GoToScene2 : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
Application.LoadLevel(2);
}
}
The Cube is set to 0 for Gravity Scale, the Is Trigger is marked, and the Kinematic on Rigidbody2D is unchecked.
Any help?
I have solved the problem. I just added the second scene (which was named 2) to the Build Settings (so now there's 3 scenes. 1, 2, 2).
Answer by DoTA_KAMIKADzE · May 03, 2015 at 08:53 PM
1) Make sure that your player actually has "Player" tag.
To verify that above^ is OK and trigger works you can:
if (other.gameObject.tag == "Player") Debug.Log("OK");
2) Make sure that in Build settings you have included your scene #2 in ScenesInBuild with position #2 on the right side of it.
Note: If you didn't know - you can use name of scene in LoadLevel instead of number.
I got it to work earlier and forgot to close this. I just had to add it (again) to the Build Settings).
And the name of the scene was actually 2 :p
Well it doesn't matter which name it had/has in your code you use integer value that means that you use position of scene in Build settings not the name of scene.