Loading /switching a new scene

Hi Guys,

I am a beginner and I am really loving Unity and what it can do, I have a little bit of a coding background and always wanted to make a game. Soooooo I decided I’ll try and make a top down 2D RPG game. Learning lots and have gotten my character to move, talk to NPCs, stop when hitting a wall or object but for the life of me I cannot get my scene to transition from entering or leaving a house.

I have watched numerous videos but cannot get this to work for my game. I am trying to use the class SceneManager.

The idea is that when the “Player” hits the door or walks into it it will transition to the next scene which will be inside the house and vise-versa.
Examples:
8945511--1227480--upload_2023-4-13_19-47-29.png8945511--1227486--upload_2023-4-13_19-48-42.png

Most videos had a “prefab” option or something to get it working but I honestly have no idea what that is and how to get that working.

The “Player” has a Box Collider 2D and Rigidbody 2D on it and the SceneTransition has a Box Collider 2D on it.

The code I am using is below, which is on the scene transition.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneTransition : MonoBehaviour
{
public string sceneToLoad;

public void OnTriggerEnter2D(Collider2D other)

{
if (other.CompareTag(“Player”) && !other.isTrigger)
{
SceneManager.LoadScene(sceneToLoad);
}
}

}

I am not sure what other information to provide, but I hope this helps:
Scene 1: OpenWorld
8945511--1227492--upload_2023-4-13_19-51-56.png
Scene 2: House
8945511--1227495--upload_2023-4-13_19-52-31.png

Thank you so much for whoever tries to help me and my journey to making a 2D RPG game :slight_smile:

8945511--1227483--upload_2023-4-13_19-48-18.png

All you’ve said is that you cannot get it working so what is not working?

Is the trigger callback happening? Add a Debug.Log(“Door hit”) to test it. Is the door “BoxCollider2D” set to be a trigger?

You can use layers so you don’t need to test tags. Layers are filtered by the physics engine to save you doing this i.e. have a layer called Player and a layer called Doors and set them to interact only with each other in the Layer Collision Matrix.

When the Player enters the door area or collision zone nothing happens.

Yes trigger is set for the door and the player (not sure if this is needed).

I am not sure how to do that sorry, I have layers for solid objects and intractables.

Is there anything wrong with the code or do I need to do anything to add in another component?

Is this where I add the bugger?

Nothing triggers when “Player” is in the zone
8945781--1227594--upload_2023-4-13_22-11-5.png

This is for the SceneTransition:
8945781--1227600--upload_2023-4-13_22-12-24.png
8945781--1227597--upload_2023-4-13_22-11-56.png

I hope this explains it better.

If you want to check if that callback is happening, add a debug.log as the first line. The above is only happening if the callback happens, it’s a “Player” and not a trigger.

Sounds like basic set-up stuff going wrong:

  • Are both these colliders on a layer that is allowed by the Layer Collision Matrix?
  • Turn on gizmos; are both colliders overlapping?

As I said above, you should use layers as it’s a physics thing. Then you won’t need to check triggers and tags.

Can you show the Player components please?

Hopefully you’re not “moving” a player by setting the Transform. This isn’t physics movement. Colliders only move by adding a Rigidbody2D and using its API.

Any collider without a Rigidbody2D is implicitly Static i.e. non-moving. Static colliders NEVER come into contact with other Static colliders are it wouldn’t make sense; they should never move.

I have put Debug.Log"Door Hit" on line 11, I hope thats correct.

I am not sure how to answer that but the “Player” is only in the OpenWorld Scene not the House Scene.

The reason I am using Scenes is so that the Player can only see the house when they are in the house and the openworld when they are roaming and only see the outside of the house not the inside of the house. I dont know how to do that using layers.

Does my player also need to exist on the House scene also? Is that where Prefabs come into play?

Regarding your last comment does that mean that my SceneTransition would also require a Rigidbody 2D component?

Pretty much trying to replicate this:

But for whatever reason using the same setup I cannot get my scene to transition.

Try doing two separate Debug.Logs, like this:

public void OnTriggerEnter2D(Collider2D other)
{
    Debug.Log("OnTriggerEnter2D called.");

    if (other.CompareTag("Player") && !other.isTrigger)
    {
        Debug.Log("Condition met.  Loading scene.");
        SceneManager.LoadScene(sceneToLoad);
    }
}

That should let you know a.) whether OnTriggerEnter2D is even being called to begin with, and b.) whether or not your condition is registering as true.

Okay I’ve done this and nothing is coming up via the console, do I need to add this to the player also? I have just added it to the SceneTransition.CS

Does it matter that I am using VSC to code not Visual Studio?
Does my PlayerController.cs need “using UnityEngine.SceneManagement;” also? I have watched so many tutorials regarding this and they are all the same I just dont understand why I cant get mine to trigger. Some used Prefabs like this video unity switching between scenes 2d unity rpg - Google Search which I have tried to do also but since I am a beginner maybe I havent setup that side correctly.

The fact that nothing whatsoever is being shown in the console indicates you’ve set up one (or both) of your objects incorrectly.

I studied your screenshots above a little more closely and, sure enough, noticed your player’s Rigidbody2D has been set to Static. Your character’s Rigidbody should instead be set to either Dynamic or Kinematic (Static Rigidbodies should not be moving around).

From the Rigidbody2D docs:

OMG IT WORKED! HOW DID I MISS THAT?! Thank you! I could kiss you!!! :wink:
Now the other issue I have now is when it loads the new scene the player is nowhere to be seen, is this an easy fix, do I now need to drop the “Player” asset into the House scene or is there a way that it will carry across from one scene to the next and back again?

Just tried DontDestroyOnLoad(colliderObject); but received the error in the Console log:
8947707--1227903--upload_2023-4-14_12-29-47.png

I don’t know. I did say that in my reply to you. :slight_smile:

Sorry! It’s my first week learning so a lot of terms go straight over my head.

1 Like