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 The-Aidency · May 01, 2014 at 06:02 AM · timeenergy

Display text when energy reaches 0

I just tried each and everything I knew, but my program wont work. It shows the "You Lose" screen just when it decreases 1 NRG point. HELP!!!

 var EnergyLoss = 1;
 var EnergyLossTiming = 10;
 var LastEnergyLossTime = 0;
 var Energy = 100;
 
 function Update(){
 if((Time.time-LastEnergyLossTime)>EnergyLossTiming)
 {
 HealthConfidenceNRG();
 LastEnergyLossTime = Time.time;
 }
 }
 
 function HealthConfidenceNRG(){
 Energy -= EnergyLoss;
 if (Energy == -1);
 {
 Time.timeScale = 0;
 LostTheGame = true;}
 Debug.Log("Energy reduced to" + Energy);}
 
 function OnGUI()
 {
 GUI.skin = newSkin;
 GUI.Box (new Rect (0,1,100,30), "Energy" + ":" + Energy.ToString());
 GUI.Box (new Rect (100,1,100,30), "Health" + ":" + Health.ToString());
 GUI.Box (new Rect (200,1,100,30), "Confidence" + ":" + Confidence.ToString());
 if (LostTheGame){
 GUI.Box(Rect(Screen.width /2 - 100,Screen.height /2 - 100,250,200), "You Lose");
         
         //Make Main Menu button
         if(GUI.Button(Rect(Screen.width /2 - 100,Screen.height /2 - 50,250,50), "Main Menu")){
             Application.LoadLevel(MenuSceneName);
         }
         
         //Make Retry button
             if(GUI.Button(Rect(Screen.width /2 - 100,Screen.height /2 ,250,50), "Retry")){Application.LoadLevel(CurrentSceneName);}
 }
 }

 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · May 01, 2014 at 06:14 AM

One problem is on line 16:

 if (Energy == -1);

The ';' at the end of the line terminates the 'if' statement. That means the rest of the code in this function will be executed the first time HealthConfidenceNRG() is called. Remove the ';' from the end of the line.

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 The-Aidency · May 01, 2014 at 02:32 PM 0
Share

Thanks dude!!!

Did not know that :p

avatar image
1

Answer by calmcarrots · May 01, 2014 at 06:39 AM

 var LostTheGame = false; //Make sure you have this
 var nextTimeUntilLoseEnergy = 0;
 var energyLossInterval = 1; //Every one second, you will lose energy
 var Energy = 100; // How much energy we have
 var EnergyLoss = 1; //How much energy we will lose
 
 function Update()
 {
    if(Time.time > nextTimeUntilLoseEnergy)
    {
      HealthConfidenceNRG();
      nextTimeUntilLoseEnergy = Time.time + energyLossInterval; //From Time.time, add any value. Once Time.time passes this value, it will call 'HealthConfidenceNRG();'
    } //This is more efficient since it uses less math
 }
  
 
 function HealthConfidenceNRG()
 {
    Energy -= EnergyLoss;
    if (Energy <= -1) //No semicolon here. Also, make sure it is set to '<=' not to '=='
    {
       Time.timeScale = 0;
       LostTheGame = true;
    }
    Debug.Log("Energy reduced to" + Energy);
 }


Let me know if you don't understand a piece of the code. Just replace all your code (except for the OnGUI function) with my code.

Comment
Add comment · Show 3 · 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 The-Aidency · May 01, 2014 at 02:34 PM 0
Share

Sorry, I know it works, but only removing the ";" worked like I wanted it.....sorry :(

However, I understand the code!

avatar image calmcarrots · May 01, 2014 at 05:55 PM 1
Share

Sure! Everything helps and hopefully you learned something new

avatar image The-Aidency · May 06, 2014 at 06:37 AM 0
Share

Yeah, I did

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

20 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

Related Questions

Energy refill script bug fix 2 Answers

Energy refill script bug fix 0 Answers

how to stop object before GO writes 1 Answer

Starting and stopping Coroutines 1 Answer

Breathing sound effect using timers 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