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)