- Home /
reset the level?
after my character dies he goes back to the main menu(for now) but when i hit play game it just flashes up the level for just a second when i press it. when i hit play game when i start the game it is fine. i'm assuming that it has to do with the level being destroyed after i go to another level.so could someone please explain how to "reset a level"?
My main menu text script:
var isQuitButton = false;
function OnMouseEnter() { //change txt color
renderer.material.color = Color.gray;
}
function OnMouseExit() { //change txt color
renderer.material.color = Color.white;
}
function OnMouseUp() { //are we dealing w/ quit button
if(isQuitButton)
{
//quit the game
Application.Quit();
}
else
{
//loadlevel
Application.LoadLevel(1);
}
}
Are you using a tutorial? You've probably missed a step. Try Application.LoadLevel(Application.loadedLevel);
Answer by Bampf · Sep 15, 2010 at 01:21 PM
I'm guessing that when the game starts again, it "remembers" the fact that the game was quit. In other words, the level is loading but whatever condition caused it to end in the first place is still true.
Are you using DontDestroyOnLoad anywhere? That's often used to pass information from one level to the next, such as how many lives the player has left. But when starting a new game you will need to set the number of lives back to 3, score back to zero, etc.
There are other ways to keep information around across levels, such as PlayerPrefs, or writing the data to a file. Whatever you are using, you will need to reset it before starting a new game.
Do you have any static variables? Same issue. Here's a link to a Q&A for a problem very similar to yours, that was caused by static variables.
Answer by vdizzle · Aug 17, 2010 at 03:42 PM
I had the same issue yesterday with my level only flash not even a sec. Its your code. Are you using a platform controller script? I did not destroy the level after i complete each or after I have died. What function do you have after the user die in you death()? Make sure you create an if then statement then load the new scene. I thought it was something far complicated but it was just the code....
so say after 3 deaths your end user have to go back to the main menu. Also you may have code in two errors....thats what my problem was. I had code in the death() in the platform controller script and also in the guitext script that controlled the amount of lives.
code
////// if (lives== 3){ Application.loadLevel(1); } }
Answer by vdizzle · Aug 18, 2010 at 12:53 AM
I think its your code. Why dont you just do this. Create a new js file. Then for your two buttons You have a quit button and then a restart button. hopefully your using GUITEXTURES--samething applies with GUITEXT
So you can break these off into 2 separate scripts and attach each to each button.
////////////////////////////////////script 1 attached to the restart btn
var btnA:GUITexture;///this is the Quit Button
function OnMouseDown(){
btnA.enabled=true;
Application.LoadLevel(1); }
///////////////////////////////////
script 2 attached to the quit button
var btnB:GUITexture;//this can be the restart button
function OnMouseDown(){
btnB.enabled=true;
Application.Quit(); }
that didn't work because i don't have anything to attach to the GUITextures. somebody please help me!
Answer by phillywassupp · Dec 12, 2010 at 05:43 PM
Hey i am having the same problem, My game works well moving to the levels but then when the player dies it loads a new scene with two buttons one which reverts to the first level and one which goes to the main menu but even when you play the first level again it just does the 1 second die thing.
Did you use the tutorials by tornado twins by any chance? i think it could be something to do with the health control as its not scene specific and isn't resetting at the start of the level.
if you figure it out i would love to know!
Hey i figured it out!!
mine works perfectly now all i had to do was reset the static variable on awake using this code:
static var LIVES :int ;
private var LIVESInt = 3;
function Awake () { LIVES = LIVESInt; }
hope it works for you
Sorry your code does not work for me !!! ' function On$$anonymous$$ouseUp () {
Application.LoadLevel(0);
}
static var LIVES :int ; private var LIVESInt = 3;
function Awake () {
LIVES = LIVESInt;
}'
Answer by GXVIII · Nov 19, 2012 at 08:56 PM
Well seeing as how it is a pain to have everything reset itself. I will be creating a transition scene. You can save your current scene name (the one being reset) in a playerpref, move to the transition scene, then from the transition scene have it load the saved playerpref level name. This may seem a bit much but loading a scene with a static loading backdrop then loading the original scene seems a lot easier. Whenever I load a new scene then load the one I want reset it seems to load fresh. However this will not fix issues with DontDestroyOnLoad objects.
Just tested my code and approach and is working flawlessly for me.