- Home /
How do I Reset My Variables On GameOver?
Hello,
I am trying to reset my variables when the player loses or wins, and he hits 'new game' Right now, just to test the code, I created a function called 'reset' which resets three variables (lives, bombs, score) to zero (for the purposes of testing). I put this code in the gamecontroller object. Which I placed in my menu scene.
I call this function when the player hits 'new game' on the menu screen. I also have a gameController object in the level that I load. I also update the GUI via script. The Reset function doesn't seem to work. Instead, it's pulling the variables directly from the GmeController script's Awake function, and not resetting them.
What am I doing wrong? Should I only have one game controller throughout my entire game? Is calling Reset before a Scene Load function the wrong way to go about it?
Game Controller Script
static var GameOver : boolean;
var Player : GameObject;
//set the player score
var score : int;
var lives : int;
var bombs : int;
//GUI Variables:
var scoreText : UILabel;
var livesText : UILabel;
var bombText : UILabel;
var gameController:GameController;
//var PowerUpSound: GameObject;
function Awake(){
lives= 1;
bombs = 1;
GameOver = false;
totalEnemies = 1 ;
isWin = false;
crabturret = 1;
DontDestroyOnLoad(this);
UpdateHUD();
}
function Update(){
//print(lives);
}
function Respawn(){
yield WaitForSeconds(.1);
Instantiate(Player, Vector3(-18.0759, 0, -1.6689), Quaternion.Euler(-90, -180,0));
}
// Ui stuff
function UpdateHUD()
{
livesText.text = "Lives: " + lives;
scoreText.text = "Score: "+ score;
bombText.text = "Bombs: "+ bombs;
}
//strt the game over
function Reset(){
lives= 0;
bombs = 0;
score = 100;
GameOver = false;
}
Answer by homer_3 · Nov 09, 2012 at 09:51 PM
If you are reloading the scene, your object you called reset on will get destroyed and a new one will be created, which will then call its awake method.
Thanks,
I think I used a Donot destroy on load command. And I placed the reset script inside the Gamecontroller script/object. So it's ok to have two GameControllers? One in each scene?
Your answer
![](https://koobas.hobune.stream/wayback/20220613083619im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
getting udp package info inside unity (GlovePIE) 0 Answers
Help with scriptableobjects 1 Answer
join two scripts to use one var in both scripts 1 Answer
How to make the level screen????? 0 Answers
Error BCE0077 1 Answer