Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by Beshkan · Feb 03, 2017 at 08:35 PM · guitimerprogress-bar

OnGUI doesn't update

I have a problem with time progress bar, It's doesn't update every moment. Just when time is less than zero it's suddenly goes empty. It's always full and doesn't decrease. sorry for my terrible english.

full time progress. alt text

empty time progress. alt text

My code:

 using UnityEngine;
 using System.Collections;
 
 public class Score : MonoBehaviour {
 
     public float time = 10;
     public float maxTime = 10;
     public float timeBurnRate = 1;
 
     public Texture2D bgTexture;
     public Texture2D timeBarTexture;
     public int iconWidth = 32;
     public Vector2 timeOffset = new Vector2 (10, 10);
 
     void Start(){
                player = GameObject.FindObjectOfType<Player>();
         }
 
     void OnGUI(){
         var percent = Mathf.Clamp01 (time / maxTime);
 
         if (!player)
             percent = 0;
         
         DrawMeter (timeOffset.x, timeOffset.y, timeBarTexture, bgTexture, percent);
     }
 
     void DrawMeter(float x, float y, Texture2D texture, Texture2D background, float percent){
         var bgW = background.width;
         var bgH = background.height;
 
         GUI.DrawTexture (new Rect (x, y, bgW, bgH), background);
 
         var nW = ((bgW - iconWidth) * percent) * iconWidth;
 
         GUI.BeginGroup (new Rect (x, y, nW, bgH));
         GUI.DrawTexture (new Rect (0, 0, bgW, bgH), texture);
         GUI.EndGroup ();
     }
 
     void Update () {
         if (!player)
             return;
         if (time > 0) {
             time -= Time.deltaTime * timeBurnRate;
         } else {
             Explode script = player.GetComponent<Explode>();
                         script.OnExplode();
         }
         }
 }

meter-air-copy.png (15.2 kB)
meter-background.png (190 B)
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 Bonfire-Boy · Feb 04, 2017 at 02:39 PM 0
Share

I don't see how that will compile, since you never declare the player variable.

1 Reply

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

Answer by Bunny83 · Feb 04, 2017 at 03:37 PM

This line:

 var nW = ((bgW - iconWidth) * percent) * iconWidth;

doesn't make much sense. You calulate the width of the background minus the iconWidth which gives you the width of the remaining background. That amount you scale with your percentage. Now comes the strange thing, why do you multiply by iconWidth? You should remove that last multiplication.

Also you seem to only scale your image instead of actually "sliding" it. Shouldn't you use that variable as x-offset?

It would help to understand how your actual images look like (timeBarTexture and bgTexture). Also what exact behaviour you want. If the time is 10, should the bar be "full"? While it's decreasing, should it move to the right? In that case you have to invert your percentage.

Basically something like this:

 void DrawMeter(float x, float y, Texture2D texture, Texture2D background, float percent){
     var bgW = background.width;
     var bgH = background.height;

     GUI.BeginGroup (new Rect (x, y, bgW, bgH));
     GUI.DrawTexture (new Rect (0, 0, bgW, bgH), background);
     
     var xPos = ((bgW - iconWidth) * (1f-percent);
     GUI.DrawTexture (new Rect (xPos, 0, bgW, bgH), texture);
     
     GUI.EndGroup ();
  }
Comment
Add comment · Show 5 · 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 Beshkan · Feb 04, 2017 at 11:33 PM 0
Share

Thanks, it's amazing. How can i do this from right and when rich to icon(square that hold "A" char) stop? I know percentage must invert but how about left icon. i want it stay.

avatar image Bunny83 Beshkan · Feb 05, 2017 at 12:41 AM 0
Share

Then it would be easier to seperate the icon and the bar. However there might be another way by reducing the clipping area on the right side.

 var bgW = background.width;
 var bgH = background.height;
 GUI.DrawTexture (new Rect (x, y, bgW, bgH), background);
  
 var nW = ((bgW - iconWidth) * percent + iconWidth;
 GUI.BeginGroup (new Rect (x, y, nW, bgH));
 GUI.DrawTexture (new Rect (0, 0, bgW, bgH), texture);
 GUI.EndGroup ();

Now that i see the result i finally understood what you wanted to do ^^. Your only error was that you have a * where you should have a +

I'm just wondering, why is the icon part of the slider when it should not move? Shouldn't it be part of the background?

avatar image Beshkan · Feb 05, 2017 at 01:22 PM 0
Share

I don't know, i see that way in this tutorial. btw you have a good solution for this problem. tnx again.

avatar image Bunny83 Beshkan · Feb 05, 2017 at 01:35 PM 0
Share

Are you sure you don't have confused a "+" with a "*"? If they really wrote * iconWidth it's an error.

avatar image Beshkan Bunny83 · Feb 05, 2017 at 01:54 PM 0
Share

Yes, It's my fault. Shame on me. :(

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

6 People are following this question.

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

Related Questions

Problems showing accurate time with FixedUpdate() 0 Answers

FPS Styled Timer Game: Script for starting a timer on play and stopping the timer when win condition achieved. 1 Answer

Timer Board. 1 Answer

¿ How can I do a countdown timer showed in GUI that restarts in each scene ? 0 Answers

How to make a timer that freezes the game after you hit resume 0 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