How to set points back to zero when reload the scene?

hello to all!

I’m making a memory card game. i flip two card objects in my game and when i click on them both (which they have the same image) i get 10points and they both get destroyed. I have a js script ScoreManager that controls the points. Also there is a timer counts and if it becomes equal to zero then a game over GUITexture appears and loads back the main menu.

This is the js script for card 1 (there is another one js script for gameobject card 2):

    function OnMouseDown () {
    card_1.transform.localRotation.z = 180;
    flipedCard = true;
    Invoke("CheckCards", 1);
    }
    function CheckCards(){
    if((flipedCard == true) && (Flip_card_2.flipedCard == true)){
    flag=true;
    Destroy(card_1);
    }
    else if(flipedCard == true){
    card_1.transform.localRotation.z =0;
    flipedCard=false;
    }
    }
    //variables
    public var card_1: GameObject;
    card_1 = GameObject.Find("card_1");
    public static var flag:boolean=false;
    public static var flipedCard:boolean=false;

ScoreManager js code below, attached to my points GUIText object:

public  var p:int;
public  var p1:int;
public var pointsClip: AudioClip;

public var flag_:boolean;

function Start(){
 p = 0;
 p1=0;
 guiText.text = "0";
 flag_ = false;
}

function Update () {

	if(Flip_card_1.flag == true || Flip_card_2.flag==true){
		p1 = 10;
		if(flag_ == false){
			audio.PlayOneShot(pointsClip);
			flag_ = true;
		}
	}
	p = p1;
	guiText.text = ""+ p;

}

The problem is after the game over GUITexture (it brings me back to main menu and) reloads my scene again and the points GUIText shows the last points i got from my last score (ex. points=10) instead of showing my points back to 0 (as i set on Start() ). Why is that happening?

you don’t want static. Static variables don’t get reset upon loading scenes and don’t get cleaned up.

Are you loading your scene additively? Does OnDestroy ever get called on the score script? It looks like the only way this is possible is if you’re not actually destroying and loading the scene again.

Personally, I prefer to manually reset the game, rather than depend on Unity’s level loading to fix everything. It also becomes necessary if you have complex and load-heavy scenes. You don’t want to make them wait 20 seconds every time they want to restart.

Just make a reset game routine/function that manually sets everything back where it should be, including the score. I like to have a Reset function in all my manager scripts. Then I have my GameManager reset all other managers in one go.

Hope this helps!

How are you reloading your scene? Remember that Start() is called only once in the lifetime of the script, when it is first enabled. If you don’t ever disable and re-enable the script, then Start() will never be called again.

You could instead try putting your variable initialization into Awake(), which does get called whenever a script is re-initialized.

Furthermore, static variables are not destroyed when you reload a scene, since of course these belong to the Class and not an instance of the class.

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Start.html
http://answers.unity3d.com/questions/63241/reload-a-scene-and-resetting-all-variables.html