1

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.

adrian
  • 97
  • 2
  • 12
  • This should not happen, loading scene should be enough to reset all objects to their initial settings. Unless the object is marked as "don't destroy on load". Or you modified something that is not an object property. What exactly do you do to make the player ignore collisions? Did you use Physics.IgnoreLayerCollision or something similar by chance? – Yuri Nudelman Nov 01 '21 at 14:01
  • Yes I do, I use Physics2D.IgnoreLayerCollision in a coroutine, I thought that reloading the scene was the best options to reset all settings but i don't understand why this happens. – adrian Nov 01 '21 at 15:02
  • Is the player a singleton or using something `static` or `DontDestroyOnLoad`? – derHugo Nov 01 '21 at 15:11
  • Yes, I used the singleton patern with the player, but reloading the scene wouldn't destroy the player? – adrian Nov 01 '21 at 16:44

1 Answers1

1

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.

Yuri Nudelman
  • 2,874
  • 2
  • 17
  • 24
  • Thank you for answering. Indeed the solution was by simply reset the ignoreLayerCollision after restarting the game. Thank you so much – adrian Nov 02 '21 at 10:11