Hi. I’m learning Unity and C#, and I’m having trouble with loading a new scene. This code works fine:
public string sceneName;
private void OnTriggerEnter2D(Collider2D collision) {
Player player = collision.GetComponent<Player>();
if(player)
{
SceneManager.LoadScene("TavernGround");
}
}
But, i want to move my player to another scene as well. So, when i do that:
public string sceneName;
private void OnTriggerEnter2D(Collider2D collision) {
Player player = collision.GetComponent<Player>();
if(player){
Scene sceneToLoad = SceneManager.GetSceneByName("TavernGround");
SceneManager.MoveGameObjectToScene(collision.gameObject, sceneToLoad);
}
}
It doesn’t work. Scene object is Null. Even if I replace var sceneName with actual name, it still doesn’t work. Neither GetSceneByBuildIndex. What am I doing wrong? Or there is other way to move player to other scene?