How do I change level/scene?
I have searched all over the internet to try and find out how I can do this but I cannot find a solution anywhere. I am using C# and am making a 2D game. I want it so that when the player enters a box colider it teleports him to a new scene. I read that you now need to use SceneManager instead of LoadLevel and this is where I am confused. The script is fine with no errors in console however, when the character goes into the collider nothing happens.I will post my script below and if anyone could help me fix it that would be great!
using UnityEngine; using System.Collections; using UnityEngine.SceneManagement;
public class LevelLoader : MonoBehaviour {
private bool playerInZone;
// Use this for initialization
void Start () {
playerInZone = false;
}
// Update is called once per frame
void Update () {
if (playerInZone)
{
SceneManager.LoadScene ("Menu");
}
}
void onTriggerEnter2D(Collider2D other)
{
if(other.name == "Player")
{
playerInZone = true;
}
}
}
please try to format ALL of your code...
your problem is that you're expecting unity to call onTriggerEnter2D()
upon collision but it's not going to. if you used OnTriggerEnter2D()
on the other hand...
you need to pay special attention to the case.
So I have changed the script as you said but it is still not working? I am not sure if I missed something else that is obvious but I am not very experienced with C#.
Here is the new script:
using UnityEngine; using System.Collections; using UnityEngine.Scene$$anonymous$$anagement;
public class LevelLoader : $$anonymous$$onoBehaviour {
private bool playerInZone;
// Use this for initialization
void Start () {
playerInZone = false;
}
// Update is called once per frame
void Update () {
if (playerInZone)
{
Scene$$anonymous$$anager.LoadScene ("02");
}
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.name == "Player")
{
playerInZone = true;
}
}
}
is ("02") a string value or you try to add int value & why do you use bool variable to load scene OnTriggerEnter2D is enough :
void OnTriggerEnter2D(Collider2D other)
{
if(other.name == "Player")
{
Scene$$anonymous$$anager.LoadScene ("$$anonymous$$enu");
}
}
Answer by prakash.jadhavjp · Mar 10, 2016 at 10:18 AM
write a debug.Log in the OnTriggerEnter2D() to ensure it is getting triggered. make sure that at least one the box or the player has rigidbody2d component to it . For a trigger to work at least one has to have the rigidbody2d component
Please try to make the player as DontDestroyOnLoad() ; so that your player gameobject is in the secound secene also .
Hope this helps