Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 FlippingFlopper · Jun 16, 2020 at 09:12 AM · countdownsingle

good way to call a method only once within countdown??

I want to call multiple methods on specific countdown time. So i've been using boolean value and change it to false when the method is called. But having a lot of methods makes me to create too many boolean values. is there any better way to make this ?

 public class DoSomethingWithCountdown : MonoBehaviour {
 
     private float timer = 100f;
 
     private bool firstBool = true;
     private bool secondBool = true;
     private bool thirdBool = true;
     // more boolean values will be needed...
     
     void Update() {
         timer -= Time.deltaTime;
 
         if (timer < 80 && firstBool) {
             // do something
             firstBool = false;
         }
 
         if (timer < 50 && secondBool) {
             // do something
             secondBool = false;
         }
 
         if (timer < 20 && thirdBool) {
             // do something
             thirdBool = false;
         }
 
         // and so on...
 
     }
 }
 
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

1 Reply

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

Answer by CorruptedTNC · Jun 16, 2020 at 09:42 AM

Yep, there are a few ways you can do this. First of all well done for identifying that there's probably an easier way of accomplishing your goal, it's a good sign that you're learning and improving!

I'd personally approach it this way: You could create a class that wraps an action, and have that class invoke the action after a specified time frame.

 using System;
 using UnityEngine;
 
 internal sealed class TimedEvent
 {
     private float m_CurrentTime;
     private float m_MaxTime;
     private Action m_Action;
     private bool m_HasRun;
     
     public TimedEvent(float time, Action action)
     {
         m_MaxTime = 0f;
         m_Action = action;
     }
     
     public void Tick()
     {
         if (m_HasRun)
         {
             return;
         }
         m_CurrentTime += Time.deltaTime;
         if (m_CurrentTime >= m_MaxTime)
         {
             m_Action();
             m_HasRun = true;    
         }
     }
 }

Which can then be used like so

 using System;
 using UnityEngine;
 
 internal sealed class SomeGameObject : MonoBehaviour
 {
     private TimedEvent m_EventOne = new TimedEvent(5f, () => Debug.Log("HELLO!"));
     // Repeat for other events, probably introduce a collection of them so you can
     // iterate them and make them process a tick all at once
     
     private void Update()
     {
         m_EventOne.Tick();
     }
 }

You could also make an event 'reset' itself every time it invokes its action if it's meant to loop.

You could even go a step further and introduce a class that wraps your events, and allow other scripts to register timed events. I recently needed a timed event system for day/night cycle and regular reoccurring events. I can't post code for this, but here's a general idea.

  • I have a class which has a Dictionary<float, Action> to hold events and their times

  • It has a public method RegisterEvent(float time, Action action) where time is between 0 (start of day) and 1 (end of day)

  • Each update cycle, check to see if we've passed the specified time

  • It's the responsibility of the action to handle behaviour like looping, not the time management class

  • Time can be fast forwarded if the day ends prematurely, and all the events will still fire

Sorry if this doesn't compile, I don't have visual studio in front of me :D

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 FlippingFlopper · Jun 16, 2020 at 01:49 PM 0
Share

Thanks !. it helped me a lot :)

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

127 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to unload only 1 texture without a performance overhead? 1 Answer

Count Down Timer.... (per second) 3 Answers

Having trouble with slowly decreasing a variable 2 Answers

Problems playing animation when time hits zero. 1 Answer

How to stop a countdown from counting down 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