Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kbresciani117 · Feb 08, 2015 at 05:56 AM · javascriptliveslifevideo game

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;
  
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VOTRUBEC · Feb 08, 2015 at 06:11 AM 0
Share

Where are you setting ballHealth.Lives? Is BallHealth a struct you've defined somewhere?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by VOTRUBEC · Feb 08, 2015 at 06:13 AM

Have you tried

 void Start ( )
 {
     ballHealth.Lives = 3;
     ...
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kbresciani117 · Feb 08, 2015 at 05:18 PM 0
Share

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?

avatar image VOTRUBEC · Feb 09, 2015 at 05:49 AM 0
Share

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".

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VOTRUBEC · Feb 09, 2015 at 05:43 AM 0
Share

Where did you get "gameObject" from?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges