- Home /
Lives aren't subtracting, going straight to game over.
Hi! I have a problem with the lives in my game. It just goes straight to the game over screen, not subtracting from the lives. Sorry for the unformatted code, it isn't copying and pasting right at the moment. :/ var Player : Transform;
static var CurrentScore : int = 0;
var MusicPrefab : Transform;
var GameOverSound : AudioClip;
var gameoverlevel : String ;
var ballHealth : BallHealth;
static var Restart = false;
function Start ()
{
CurrentScore = 0;
if (!GameObject.FindGameObjectWithTag("MM"))
{
var MM = Instantiate(MusicPrefab, transform.position, Quaternion.identity);
MM.name = MusicPrefab.name;
}
}
function OnGUI () {
GUI.Box ( new Rect (5 ,0, 80, 50), "Score: " + CurrentScore);
GUI.Box (new Rect ((Screen.width - 85) ,(Screen.height) - (Screen.height), 80, 50), "Lives:" + BallHealth.Lives);
}
function RestartLevel()
{
audio.clip = GameOverSound;
audio.pitch = 1;
audio.Play();
yield WaitForSeconds (audio.clip.length);
transform.position = Checkpoint.ReachedPoint;
ballHealth.BallColor();
ballHealth.Lives -= 1;
if (ballHealth.Lives == 0)
{
Restart = true;
CurrentScore = 0;
Application.LoadLevel(gameoverlevel);
ballHealth.Lives = 3;
Restart = false;
}
Where are you setting ballHealth.Lives
? Is BallHealth
a struct you've defined somewhere?
Answer by VOTRUBEC · Feb 08, 2015 at 06:13 AM
Have you tried
void Start ( )
{
ballHealth.Lives = 3;
...
}
I tried that and it's still going straight to game over. I have the lives defined in the BallHealth script.
static var Lives = 3;
Do you think if I just move it to the Game $$anonymous$$anager script it'll work better?
Throw in a few Debug.Log("ballHealth.Lives = " + ballHealth.Lives);
statements through the code. For example, line 56 would be a good place for one. I'd also try throwing one in at line 29.
You can also select "Debug" in the Inspector window. Look for the Inspector tab, then on the right hand side, there's a dropdown menu. Select Debug. That'll give you a whole lot more information about what's happening with your objects. If you select your object that has the BallHealth attached, or even possibly the Player object, you'll see all sorts of new information without cluttering up your code with too many Debug.Log statements. Then you can pause the game at certain areas to see what's going on.
But the BEST solution would be to move over to C#, on a Windows machine, install Visual Studio Community 2013, install UnityVS plugin, then you can actually set breakpoints and look at the code execution in "real-time".
Answer by meat5000 · Feb 08, 2015 at 05:40 PM
function Start()
{
ballHealth = gameObject.GetComponent(BallHealth);
ballHealth.Lives = 3;
//Other stuff
}
I don't think it should be static.
Your answer
Follow this Question
Related Questions
game won't work in build 0 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
turning script on after time 2 Answers
Check if List contains specific object values (JS) 1 Answer
Java Lists 1 Answer