- Home /
scene won't reset after restart
Hi,
I'm new to Unity and have a problem with the game I'm trying to make. I have 4 scenes. "Menu", "Game", "You win" and "Game Over". Basically a car is driving around and when 10 coins get collected the "You Win" screne appears. There I added a button which lets you restart the game.
The problem I'm having is that whenver I press the restart button the "Game" scene won't go back to its original state, which means that the 10 coins are still colelcted and therefore the "You Win" screen appears immediately after I press the restart button.
This is the code:
ragma strict
static var coinCounter = 0;
function Start () {
}
function Update () { if(coinCounter == 10){ Application.LoadLevel("You Won"); }
}
function OnCollisionEnter (collision : Collision) { if(collision.transform.name == ("Coin")) { coinCounter += 1; } }
I added all scenes to the build settings so that's not the problem. Does this maybe have anything to do with the static variables?
Thanks A LOT for any help. :)
Saskia
Answer by YoungDeveloper · Feb 23, 2014 at 09:43 PM
You shouldn't really use static for such simple things. And to me it looks like you can only collect 1 coin before level you won is loaded, because of:
if(coinCounter == 1){ Application.LoadLevel("You Won");
And yes, static variable is mostly the cause, either set it back on awake, or even better, dont use static.
Yeah sorry I changed it to 1 for testing reasons. Can you show me how to code this without static?
This should be fine. Couple notes though, i suggest using tags, ins$$anonymous$$d of comparing gameobject names, because by that you can have multiple items under one tag "pickable", or other. There is no need to have empty start or update methods if you dont use them. Also, i think you should destroy the coin after you have counted it in.
var amount: int = 0;
function Update(){
if(coinCounter == 10){
Application.LoadLevel("You Won");
}
}
function OnCollisionEnter(collision : Collision){
if(collision.transform.name == "Coin"){
coinCounter++;
Destroy(col.gameObject); //or just col, because it return gameobject
}
}
And format your code next time please, there is a button "1010" )
Thanks very much!!! Yeah I am actually destroying the coins, just didn't copy that part of the code and I'll make sure to format next time. :) Everything works now! :)
Your answer
Follow this Question
Related Questions
,Restart Scene after level Completion 0 Answers
i have a button to restart the same scene but it dont works (with images) 7 Answers
Restart Game from Button, not SceneMangement 1 Answer
Current Level Load Again After Level Completion 2 Answers
how to stop the whole game but not the death animation when player dies 3 Answers