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 Tastman · Mar 12, 2015 at 02:41 PM · guitimer

Start a timer after instantiating a game object

I'm working on a variant of the Space Shooter game from Unity's tutorial. I have created a power-up object that instantiates a shield for your ship when touched, and I'm trying to implement a bar that indicates how long the shield will last. The bar itself is working fine, but I can't get the bar to start depleting the moment that the shield is instantiated.

My code for the GUI and shield looks like this:

     private bool pup = false;
     public float barProgress;
     public Vector2 pos = new Vector2(10, 50);
     public Vector2 size = new Vector2(20, 250);
     public Texture2D emptyTex;
     public Texture2D fullTex;
 
     void OnGUI()
     {
         if (pup)
         {
             GUI.BeginGroup(new Rect(pos.x, pos.y, size.x, size.y));
             GUI.Box(new Rect(0, 0, size.x, size.y), fullTex);
 
             GUI.BeginGroup(new Rect(0, 0, size.x, size.y * barProgress));
             GUI.Box(new Rect(0, 0, size.x, size.y), emptyTex);
             GUI.EndGroup();
             GUI.EndGroup();
         }
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.tag == "pup")
         {
             GameObject go = Instantiate(shield, shieldSpawn.position, shieldSpawn.rotation) as GameObject;
             go.transform.parent = GameObject.Find("Power Ups").transform;
             audio_shieldUp.Play();
             audio_shieldDown.PlayDelayed(shieldDuration);
             Destroy(GameObject.FindWithTag("Shield"), shieldDuration);
             Destroy(other.gameObject);
             pup = true;
         }
     }
 
     void Update()
     {
         if (pup)
         {
             barProgress = Time.time / shieldDuration;
         }
     }

The shield instantiates and the bar shows up on screen, but the "barProgress" counter has already started counting and depletes too soon. What am I doing wrong here?

Also, any suggestions for how to remove the bar after shieldDuration has ended, as it's drawn in the script and not instantiated?

Comment
Add comment · Show 3
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 AlwaysSunny · Mar 12, 2015 at 05:04 AM 0
Share

I suggest you strongly consider familiarizing yourself with Unity 4.6+'s UI system. The GUI calls you're using are considered legacy.

I believe your problem involves using Time.time as the numerator for this division. Time.time increments every frame and would therefore produce the results you describe.

Time-sensitive operations are usually ideal candidates for Coroutines. You could start the routine when you collide with the powerup, create-or-enable your bargraph object, allow it to run for X seconds, affecting your game and the bargraph progress, then ter$$anonymous$$ate itself, destroying-or-disabling the graph. Simple and clean. :)

avatar image Tastman · Mar 12, 2015 at 12:20 PM 0
Share

How, though? What I don't get is why the Time.time ignores its parenting if-statement and ins$$anonymous$$d immediately starts counting when I start the game, even if I declare that barProgress = 0 the frame before.

avatar image Tastman · Mar 12, 2015 at 11:16 PM 0
Share

Ah, using deltaTime to add time values to the barProgress variable was the solution. Thanks for the heads up!

1 Reply

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

Answer by AlwaysSunny · Mar 12, 2015 at 04:38 PM

Time.time isn't what you think it is, apparently. It doesn't start accumulating when you ask for it; it is always accumulating from the moment gameplay begins. Time.deltaTime returns how much time has passed in the last frame, which can be used to drive timer logic. Look into how to code timers, and how to use Coroutines. Best,

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

how to create race lap timer, start to finish, display gui? 1 Answer

How do you write a Pauseable-Timer Script in Unity JavaScript? 1 Answer

How to set text(timer) to a fixed place 1 Answer

GUI float value display 1 Answer

how to I reset a health or progress bar 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