Search code examples
c#unity-game-enginescene-manager

Load Scene is not restarting the scene properly


I'm trying to restart my game when the player loses, I play a coroutine which makes the player ignore collisions and falling off the platforms. Then when the player click a button, the same scene is reloaded.

SceneManager.LoadScene("GameScene")

But when the scene loads again, the player is still ignoring collisions and falls, it's like the scene is loaded but not the same way when the game is played for the first time.

¿How can I reload the scene properly without closing the aplication and opening it again?

Thank you.


Solution

  • The problem is that you are using Physics2D.IgnoreLayerCollision for this.

    This property is global, it affect all scenes, and not related to the specific scene. What SceneManager.LoadScene resets is only property related to the specific scene or the objects in the scene.

    You have 2 options here:

    1. Don't use IgnoreLayerCollision. An alternative may be, for example, to disable all colliders on the player. You can use GetComponentsInChildren for example to find all the colliders.

    2. Reset IgnoreLayerCollision manually.