• 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 RenatoB · May 26, 2011 at 05:48 PM · time bartimebar

Some help with time bar

I need to put a bar (GUI) to decrease value when the player hit a poison floor.

The script works but it is very fast. Any suggestions?

Script:

 var bar : boolean;
 var maxlife = 100;
 var currlife = 100;
 
 function Start(){
 bar = false;
 }
 
 function Update(){
     if (bar == false){
     currlife = 100;
 }
 
 
 else
     currlife -= 1.0 * Time.deltaTime;
 }
 
 function OnTriggerEnter(){
 bar = true;
 }
 function OnTriggerExit();
 bar = false;
 }

Another doubts:

  • How to memorize the lat value os de currlife variable?

  • How to change the gui.box (rectangle) color from script?

Thanks a lot


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 flaviusxvii · May 26, 2011 at 05:54 PM 0
Share

Format your code with the "101010" button!

4 Replies

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

Answer by homeros · May 26, 2011 at 05:57 PM

-The script works but it is very fast *if you decrease the value of 1.0 to something smaller, it'll decrease slower.

-How to memorize the lat value os de currlife variable?

 if (bar == false){ currlife = 100; }

if you delete this, it will remember the last value.

-How to change the gui.box (rectangle) color from script? use a GUIStyle to set a background texture on box

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 RenatoB · May 26, 2011 at 06:06 PM 0
Share

Thank, Speed. You are very fast. I discover an erro in the final: DivideByZeroException: Division by zero.

I think that I´ve to finish with some action when the value = 0. So, I´ve to finish the game and reset the bar, right? To finish the game I need do open a menu level and to reset the bar I change the value to 100, is it?

avatar image
0

Answer by flaviusxvii · May 26, 2011 at 05:55 PM

If it is too fast then throttle it!

 currlife -= 1.0 * Time.deltaTime * 0.1; // 1/10th as fast..
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 RenatoB · May 26, 2011 at 06:06 PM 0
Share

Thanks. I will do it.

avatar image RenatoB · May 26, 2011 at 06:20 PM 0
Share

I inserted " 0.1" and tried " 0.01" too, and nothing happens. What is wrong?

avatar image
0

Answer by aldonaletto · May 26, 2011 at 06:12 PM

The script shown seems incorrect - there's a { missing on OnTriggerExit function. The rest seems ok - currlife should be decreased 1.0 units per second.

I suggest you to use a "static" variable to store player data. You should put all player related variables in one script, declare them as static and assign the script to the player - for instance:

static var currlife: float;
The static variables in this case can be accessed by any js script:
  Control.currlife -= Time.deltaTime;
If you have problems trying to access js static variables from C# scripts, put the C# scripts in a new folder at the root level of your project, and they will "see" the js globals.
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 RenatoB · May 26, 2011 at 06:34 PM 0
Share

how to decreased 1.0 units per second? I changed the comand to "currlife -= 1.0 Time.deltaTime 0.1;" and nothing happens.

avatar image
0

Answer by RenatoB · May 26, 2011 at 06:47 PM

Refering to time, I changed the command to

 currlife -= 0.1 * Time.deltaTime;

 currlife -= Time.deltaTime * 0.1;

and nothing happens. What´s wrong?

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Character interaction & Time bar. 0 Answers

Time based calls 0 Answers

Help with timebar 2 Answers


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