- Home /
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?
I set my variables p and p1 to static another way to say that when my scene starts again initialize both variables to 0.
public static var p:int=0;
public static var p1:int=0;
plus i put on comments function Start() But still it doesn't work though :(( Anything yet?
Do the same in your card script
function Start()
{
flag = false;
flipedCard = false;
}
He actually resets it all in the Start. Well, I would use normal variables during the scene and only if you win it then pass the value to a static variable.
Fafase, I believe his Update() function is resetting that value. for some reason "if(Flip_card_1.flag == true || Flip_card_2.flag==true)" either one of this flag remains true. Hence he is getting p as 10 when loaded
Answer by OP_toss · Dec 12, 2013 at 07:16 PM
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!
Answer by guitarxe · Dec 12, 2013 at 07:56 PM
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
Yep, said all that above, and agreed. Start will only run once, and before any Update is called, so Update shouldn't be the problem.
But you're wrong about Awake. It is also only called once during the lifetime of a script, before Start. As per the docs:
Awake is called only once during the lifetime of the script instance.
OnEnable/OnDisable is called whenever a script is enabled/initialized or disabled, if that's what you're looking for.
I still think the right answer here is to reset the scene manually. It can't be more then setting a few variables. And it will happen much faster than it takes to reload the scene. Converting my comment above to an answer.
Your answer
Follow this Question
Related Questions
how do i attach GUITEXT to a prefab 1 Answer
Score & High Score logic doesn't work 2 Answers
Display variables in GUIText? 1 Answer