Resetting All Variables in One Go?

Hello

I am making a retry button and I need to reset all the variables in the scene when I load it again. Is there any way to do this in one script or do I need to go into every script and reset them all manually?

Thank you

Anything non-static and I suppose non-scriptable object will reset its state when you load the scene again. If by load the scene again you mean with SceneManager.

If you are using static values or scriptable objects, then those will need to be done manually in some way or another.

1 Like

You could call an Event called … OnReset for example, and attach all Reset scripts to that event. If you call that one Event, all other Methods subscribed to will be performed.

Something like that:

public class Reset : MonoBehaviour
{
  public static Action OnReset;

  public void ResetAll() => OnReset?.Invoke();
}

public class SomeClass : MonoBehaviour
{
  public int HP = 0;
  public string RandomName;

  private void Awake() => Reset.OnReset += ResetValues;

  private void ResetValues()
  {
    HP = 0;
    RandomName = "";
  }
}

SceneManager.LoadScene(SceneManager.GetActiveScene().name);

should reload your scene and variables.

If for example you had modified variables like “Physics.gravity”, you do :

SceneManager.LoadScene(SceneManager.GetActiveScene().name);
Physics.gravity = new Vector3(0, 0, 0); // here indicate the value of vector 3 reset (0.0.0 is an example)

}```