2D Colliders and changing levels.

Trying to get a collider working to when the player walks into it changes level. No compile errors but still not working. I am trying to get this so the player can visit the houses within the village I have created. I am aware of slight code variation for 2D and 3D games so this could be my issue?

using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;

public class HouseLoader : MonoBehaviour

{
    public CompletePlayerController controller;
    public string House_Blue_Bottom_Right;
    public string House_Blue_Top_Right;

    void OnTriggerEnter2D(Collider2D Colider)
    {
        controller = (CompletePlayerController)transform.GetComponent("CompletePlayerController");
        if (Colider.gameObject.CompareTag("Player"))
        {
            SceneManager.LoadScene("House_Blue_Bottom_Right");
            SceneManager.LoadScene("House_Blue_Top_Right");
        }
    }


}

BUMP

Right now your code would be loading one scene, then another immediately. You would end up in the “House_Blue_Top_Right” scene, and the “House_Blue_Bottom_Right” scene would be skipped.

Are you certain that the trigger is being called? Try using “Debug.Log(“triggered”)” inside that function so you know that the trigger is being activated. Debug.Log will print a line into the console window in unity.

Right now its loading nothing. I only put the both in because I thought I could just have a script for all level transitions.

I’m assuming you have many different houses, each of which goes to a different scene. So what you should do is give each House a script like this:

using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneChangeTrigger : MonoBehaviour
{
    public string sceneName;

    private void OnTriggerEnter2D(Collider2D other)
    {
        if(other.gameObject.CompareTag("Player"))
        {
            SceneManager.LoadScene(sceneName);
        }
    }
}

Put that on the house, and when the gameobject tagged “Player” triggers the collider component, the scene will change to the scene name you put in the inspector for that house.

Make sure the object has a collider with the Trigger checkbox checked.

Not got it working just yet, when you say on the house do you mean on the actual scene of that house or just a game object?

Currently I have a box collider with the script attached.

EDIT:

What do you mean by inspector??

By house I mean the object with the collider that your player will touch to trigger the scene change.

The inspector is the details panel that appears in Unity when you select something in the editor. It’s where the list of components are, and you can setup default values for variables there.

Since I gave that script a “public string sceneName” variable, whenever a gameObject has that script on it, you will be able to enter a string for that variable in the inspector. Most variables will show up in the inspector if you make them public.

For each one of your collider objects that should change the scene, you can add the “SceneChangeTrigger” component, then in the inspector you enter the name of the scene it should change to.

Let me know if I’ve lost you.

Im lost because I understand what your saying but its not working, not sure if I’ve missed a step or what.

I’ve added that script to a asset that has a collider that is also ‘isTrigger’ and my character is tagged at Player

Edit:

Is it possible the two colldiers are not on the same layer? so that the collision isn’t registered?

Thank you for your help, and for your script!
Managed to figure out what was wrong, apparently I needed a rigidbod2d on my level loader and also to make it kinematic. :slight_smile:

Again thanks for your help :slight_smile:

No problem, to explain that a little bit more (which you may have learned already): To have a collision, both objects must have a collider, and at least one must have a rigidbody (usually the player in this scenario, but it doesn’t matter). Setting the rigidbody to kinematic is correct as that makes it so that the object does not use the physics engine to move about, and with the collider as a trigger it won’t physically block anything.

When in doubt, either use breakpoints and the debugger in MonoDevelop or Visual Studio, or put “Debug.Log()” statements everywhere to print things to the console in unity. That way you know exactly where the code failed to execute. In your case, you would have seen that the Trigger function did not get called.

I’m glad you dug in and figured it out for yourself, nice job! Let me know if you have any more questions about the code.

1 Like

Thanks for the information, its a lot clearer now. Where do I put “Debug.Log()” in the script?

It’s just a temporary line for debugging information to the console. You put them anywhere you want to be verify is getting executed. It’s good to delete them once you don’t need them anymore, because they can cause lag in the editor if you’re logging too many things in quick succession.

So you could do this to find out what code is being reached in your function:

using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneChangeTrigger : MonoBehaviour
{
    public string sceneName;
    private void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log("SceneChangeTrigger has been triggered!");
        if(other.gameObject.CompareTag("Player"))
        {
            Debug.Log("It was the player!");
            Debug.Log("Changing scene to: " + sceneName);
            SceneManager.LoadScene(sceneName);
        }
    }
}

If everything is working correctly you would see the console output:

“SceneChangeTrigger has been triggered!”
“It was the player!”
“Changing scene to: Scene_House_Blue_03” (or whatever your scene name is)

But if you don’t see one of those lines, you know where to look for problems.

1 Like