Dictionary data not resetting when switching scenes

Hi all,

Currently trying my hand at a very basic RPG prototype and am currently struggling with switching scenes when player enters a door.

I am using grid based movement, a raycast is fired to detect a door layer, when door layer is detected it takes the players position, does a dictionary lookup and loads the correct scene.

This works great when the player is in the starting scene. If player hits a door to a house the house scene is loaded.

However, it stops working when player is in house and tries to load scene back to the starting zone.

I have a ZoneManager gameobject that contains “Door” scritpable objects and a doormanager script in the starting zone.

The house has a new zonemanager gameobject that contains a “Door” scriptable object and a doormanager as well, so there is no carryover of the gameobject.

I’ve noticed that the following through debugs when player hits a house door:

  • On Awake, it shows that the scriptable object is found when player is in the house scene.

  • In the CacheDoorsInZone() function, it shows me that - Player House Inside Door was added to doorcache using Vector2 of (-3.50, -3.50)

  • When I loop through the doorcache dictionary, it shows me that only the doors from the previous scene exist in the doorcache.

Here is my code:


public class DoorsManager : MonoBehaviour
{
    [SerializeField] public Doors[] doors;
    public Dictionary<Vector2, Doors> doorCache; 

    private void Awake()
    {
        doorCache = new Dictionary<Vector2, Doors>();
        CacheDoorsInZone();
        foreach (Doors door in doors)
        {
            Debug.Log(door.doorName);
        }
    }

    //Cache door scriptable objects on zone manager for O(1) retrieval in GetDoorScene()
    private void CacheDoorsInZone()
    {
        foreach(Doors door in doors)
        {
            doorCache.Add(door.doorLocationInWorld, door);
            Debug.Log(door.doorName + " was added to doorcache using Vector2 of " + door.doorLocationInWorld);
        }
    }
    //Called from CollisionDetection.cs - Loads the scene of the door player just entered.
    public void GetDoorScene(Vector2 doorCordinates)
    {
        foreach(KeyValuePair<Vector2, Doors> door in doorCache)
        {
            Debug.Log(door.Value.doorLocationInWorld + door.Value.doorName);
        }
        SceneManager.LoadSceneAsync(doorCache[doorCordinates].sceneName); 

        
    }
}

Here is the output of the debugs, last debug is when the player makes contact with door.

(-4.50, 3.50)Player House Outside Door
UnityEngine.Debug:Log (object)
DoorsManager:GetDoorScene (UnityEngine.Vector2) (at Assets/Scripts/World/DoorsManager.cs:38)
CollisionDetection:HandleDoorCollision (UnityEngine.Vector2) (at Assets/Scripts/Controllers/ControllerHelpers/CollisionDetection.cs:45)
PlayerController:MovePlayer () (at Assets/Scripts/Controllers/PlayerController.cs:110)
PlayerController:Update () (at Assets/Scripts/Controllers/PlayerController.cs:69)


(3.50, 6.50)SZ-House-02
UnityEngine.Debug:Log (object)
DoorsManager:GetDoorScene (UnityEngine.Vector2) (at Assets/Scripts/World/DoorsManager.cs:38)
CollisionDetection:HandleDoorCollision (UnityEngine.Vector2) (at Assets/Scripts/Controllers/ControllerHelpers/CollisionDetection.cs:45)
PlayerController:MovePlayer () (at Assets/Scripts/Controllers/PlayerController.cs:110)
PlayerController:Update () (at Assets/Scripts/Controllers/PlayerController.cs:69)

Player House Inside Door was added to doorcache using Vector2 of (-3.50, -3.50)
UnityEngine.Debug:Log (object)
DoorsManager:CacheDoorsInZone () (at Assets/Scripts/World/DoorsManager.cs:30)
DoorsManager:Awake () (at Assets/Scripts/World/DoorsManager.cs:17)


Player House Inside Door
UnityEngine.Debug:Log (object)
DoorsManager:Awake () (at Assets/Scripts/World/DoorsManager.cs:20)


(-4.50, 3.50)Player House Outside Door
UnityEngine.Debug:Log (object)
DoorsManager:GetDoorScene (UnityEngine.Vector2) (at Assets/Scripts/World/DoorsManager.cs:38)
CollisionDetection:HandleDoorCollision (UnityEngine.Vector2) (at Assets/Scripts/Controllers/ControllerHelpers/CollisionDetection.cs:45)
PlayerController:MovePlayer () (at Assets/Scripts/Controllers/PlayerController.cs:110)
PlayerController:Update () (at Assets/Scripts/Controllers/PlayerController.cs:69)



(3.50, 6.50)SZ-House-02
UnityEngine.Debug:Log (object)
DoorsManager:GetDoorScene (UnityEngine.Vector2) (at Assets/Scripts/World/DoorsManager.cs:38)
CollisionDetection:HandleDoorCollision (UnityEngine.Vector2) (at Assets/Scripts/Controllers/ControllerHelpers/CollisionDetection.cs:45)
PlayerController:MovePlayer () (at Assets/Scripts/Controllers/PlayerController.cs:110)
PlayerController:Update () (at Assets/Scripts/Controllers/PlayerController.cs:69)



KeyNotFoundException: The given key '(-3.50, -3.50)' was not present in the dictionary.
System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <321eb2db7c6d43ea8fc39b54eaca3452>:0)
DoorsManager.GetDoorScene (UnityEngine.Vector2 doorCordinates) (at Assets/Scripts/World/DoorsManager.cs:40)
CollisionDetection.HandleDoorCollision (UnityEngine.Vector2 doorLocation) (at Assets/Scripts/Controllers/ControllerHelpers/CollisionDetection.cs:45)
PlayerController.MovePlayer () (at Assets/Scripts/Controllers/PlayerController.cs:110)
PlayerController.Update () (at Assets/Scripts/Controllers/PlayerController.cs:69)

Wait, the door has the name of the scene to load. Why does your CollisionDetection.cs have to go into a global cache to see what the door at that position leads to? Can’t it just look at the door your pressumably collided with?

I’m guessing that this is an ordering issue, or that you’re keeping a reference to a dead element in DontDestroyOnLoad or something, but before digging into that I’m wondering if maybe you can just delete the entire file and just do SceneManager.LoadSceneAsync(doorIJustCollidedWith.sceneName))?

Im a bit confused haha, sorry I’m still pretty new to all this.

To answer

“Why does your CollisionDetection.cs have to go into a global cache to see what the door at that position leads to? Can’t it just look at the door your pressumably collided with?”

I am using the tilemap and composite collider, the doors themselves don’t have any data on them, it’s a scriptable object in a manager that says - if player is at vector2 coordinate associated with me then load scene.

Would it be smarter for me to remove the doormap tilemap collider and composite collider and just add box colliders around each door as a trigger?

wondering if maybe you can just delete the entire file and just do SceneManager.LoadSceneAsync(doorIJustCollidedWith.sceneName))?

Not sure what you mean by this, I guess you mean if the doors has individual colliders? Im working with a tilemap dont know if thats possible.

Ah, if there’s no colliders on them then then the way you have done it kinda makes sense.

I assume that what’s happening is that doors are going into the wrong caches. I’d try to update your logs to see what’s going on:

Debug.Log(door.doorName + " was added to doorcache using Vector2 of " + door.doorLocationInWorld + " in scene " + gameObject.scene.name + " on DoorsManager " + GetinstanceID(), this);

and

try {
    SceneManager.LoadSceneAsync(doorCache[doorCordinates].sceneName); 
}
catch (KeyNotFoundException) {
    Debug.LogError("Key " + doorCordinates + " not found by DoorsManager " + GetInstanceID() + " in scene " + gameObject.scene.name);
}

GetInstanceID is unique every time you load a object, so it’ll be different for the same object if you load it’s scene, unload it, and then load it again.

I couldn’t resolve it that way, to make my life easier I did create a door prefab with a collider and a script and just put it in the location of the door, it now works for inside and outside, code is a lot easier to read to :slight_smile:

public class DoorTrigger : MonoBehaviour
{
    [SerializeField] private Doors door;
    
    public void OnTriggerEnter2D(Collider2D collision)
    {
        SceneManager.LoadSceneAsync(door.sceneName); 
    }

}

I made the player kinematic and the doors static to trigger the events.

Now I got a new problem Ill need to look into. When player goes in building there is no duplicate player, when he exits however there is two of him now.

And thanks for the help, appreciate it!