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 Simlucrato · May 13, 2013 at 06:45 PM · guistatic variable

See the battery on GUI

I have this script : using UnityEngine; using System.Collections; [RequireComponent(typeof(Light), typeof(AudioSource))] public class flashlight : MonoBehaviour { public AudioClip clickSound; public float batteryLifeInSec = 300f; public float batteryPercentage = 100; private bool on; private float timer; void Update() { Light lite = GetComponent(); timer += Time.deltaTime; if(Input.GetKeyDown(KeyCode.F) && timer >= 0.3f && batteryPercentage > 0) { on = !on; audio.PlayOneShot(clickSound); timer = 0; } if(on) { lite.enabled = true; batteryPercentage -= Time.deltaTime * (100 / batteryLifeInSec); } else { lite.enabled = false; } batteryPercentage = Mathf.Clamp(batteryPercentage, 0, 100); if(batteryPercentage == 0) { lite.intensity = Mathf.Lerp(lite.intensity, 0, Time.deltaTime * 2); } if(batteryPercentage > 0 && batteryPercentage < 25) { lite.intensity = Mathf.Lerp(lite.intensity, 0.5f, Time.deltaTime); } if(batteryPercentage > 25 && batteryPercentage < 75) { lite.intensity = Mathf.Lerp(lite.intensity, 0.8f, Time.deltaTime); } if(batteryPercentage > 75 && batteryPercentage <= 100) { lite.intensity = Mathf.Lerp(lite.intensity, 1, Time.deltaTime); } } } I would like to see the variable batteryPercentage on GUI , i tried to call the method OnGUI() but there is an error , and i put the function after the update function what am i doing wrong ?

Comment
Add comment · Show 2
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 TonyLi · May 13, 2013 at 06:58 PM 1
Share

What is your current OnGUI()?

What is the error message?

avatar image Bunny83 · May 13, 2013 at 08:15 PM 0
Share

@TonyLi: I would give you an upvote for each question if possible... :D

At the moment this is just a badly formatted and badly worded "write me some code" - question.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by TonyLi · May 13, 2013 at 07:06 PM

For a text label (e.g., "Battery: 100%") try:

 void OnGUI() {
     GUI.Label(new Rect(25, 25, 200, 30), string.Format("Battery: {0}%", batteryPercentage));
 }

For an energy bar try:

 public Texture barBackground; // Assign a texture for the battery bar background
 public Texture barForeground; // Assign a texture for the battery bar foreground
 
 void OnGUI() {
     GUI.DrawTexture(Rect(25, 25, 100, 30), barBackground);
     GUI.DrawTexture(Rect(25, 25, 100 * batteryPercent, 30), barForeground);
 }


Warning: untested code. I'm not near Unity at the moment.

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.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Window Background Image not showing 1 Answer

GuiTexture Width Change 1 Answer

NGUI: Duplicate/Rotate? 0 Answers

How to Convert each 2D Array to GameObject 2 Answers

On hover GUI text change position. 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