I’m currently working on switching between scenes and respawning the player at the new scene but for some reason my gameobject is still referencing the old scenes set of gameobjects.
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
mcp.changeRoom(targetSceneName, targetSceneDoorID);
}
}
This is my “Door” code. targetSceneName is a string of the scene name, targetSceneDoorID is the id I’ve given each door so its unique, and mcp is my script that I’ve set as DontDestroyOnLoad. the mcp manages all the interactions between the major gameobjects and scripts.
public void changeRoom(string sceneName, string doorID)
{
SceneManager.LoadScene(sceneName, LoadSceneMode.Single);
//Find all Doors, then target the one with the doorID
Door[] allDoors = GameObject.FindObjectsOfType<Door>();
Door targetDoor = null;
foreach (Door door in allDoors)
{
if (door.ID == doorID)
{
targetDoor = door;
break;
}
}
//Disable so the player doesn't instantly re-enter the door
targetDoor.enabled = false;
//Reposition player
setSceneVariables(targetDoor.transform);
}
So this is my changeRoom function within my MainController.
private void setSceneVariables(Transform playerStartingPoint)
{
player = Instantiate(entityLoader.el_player, playerStartingPoint.position, playerStartingPoint.rotation);
cameraController = cam.GetComponent<CameraController>();
playerController = player.GetComponent<PlayerController>();
playerController.setUpPlayerController(this, btn_move.GetComponent<ButtonEX>(), btn_action.GetComponent<ButtonEX>(), joyStick);
cameraController.setUpCamera(this, player);
}
And this is just my function for setting up player positions also in my MainController
Whats suppose to happen is once the player hits the door trigger, the MainController is told to change the scene with both the new scenes name and the door which the player should then be spawned at being passed. In the MainController, first the new scene is loaded and then all the GameObjects that have the Door script are gathered. This array of Doors are then searched to find the one with the required ID and once its found Instantiate the player at its location.
Weirdly, my MainController is searching for the door before the scene is even loaded. I made sure the LoadScene mode was set to single. I even decided to add a door in the first scene that had the same id as the target door in the second scene and a clone of the player popped up at that door and then the new scene loaded.
I’m coding in C# and i’m on unity version 2019.2.9f1 Personal.