- Home /
Reset static variables
How can I reset a static variable? For my game i have a reset button however it does not reset my static variable. my static variable is:
public static int Score = 0;
and it is being called at:
void OnCollisionEnter() { Destroy(gameObject); Player.Score += 100; } }
You should first modify your variable and then destroy the object itself. However, it doesn't seem that the variable is been resetted, but incremented by 100.
It increases the score by 100 each time you hit a enemy. and one you die i made a reset button but when the reset button is hit it wont reset the score.
So you should post that specific part of code, this doesn't seem too much to help.
using UnityEngine; using System.Collections;
public class restart : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.R))
{
Application.LoadLevel ("Cube Game");
}
}
}
// this script helps restart the game when the R key is presed. // this alows the player to get right back into the action of the gamme instantly // is not reseting static variavbles though. // this may be resolved with another scrip.
Does that help?
Answer by BiG · Jul 15, 2013 at 10:32 AM
OK, this two answers should point you to the right direction with ease:
http://answers.unity3d.com/questions/180515/applicationloadlevel-not-resetting.html
http://answers.unity3d.com/questions/36152/reset-a-static-variable.html
It would be nice if I could get the first example in C# and not java. http://answers.unity3d.com/questions/180515/applicationloadlevel-not-resetting.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613110133im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Static variables and Design Fundamentals (singleton) 1 Answer
how to keep static variables or 2d array value 0 Answers
Static Variables 2 Answers