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 Gnarlax · Dec 04, 2014 at 01:52 PM · timercountdowncall

How to use the same script on different objects at the same time?

I have created a timer script called rt_timer.cs where It will count down based on the given seconds the user has inputted (ie. 3600 = 1:00:00)

The problem I'm facing right now is that I would like to use this script mutiple times. For example I have itemA and itemB, I can only spawn the items if the cool time is finished. itemA has 60seconds or 1 minute and itemB has 120 seconds. I could have coded the timers on each item but that would be not efficient.

Here is the code of my simple timer:

         float hours = 0f;
     float minutes = 0f;
     float seconds = 0f;
 
     public float totalSeconds; //User can specify what duration of the timer here
 
         void Update () {
         minutes = Mathf.Floor (totalSeconds / 60); //Converts totalSeconds to minutes
         hours = Mathf.Floor (minutes/ 60); //Converts totalSeconds to hours
         seconds = totalSeconds-minutes*60; //Converts totalSeconds to seconds
         totalSeconds -= Time.deltaTime; //deducts one second
         Debug.Log (hours.ToString () + ":" + minutes.ToString () + ":" + (Mathf.Floor (seconds)).ToString()); //Displays current time left
     }



Thanks in advance to those who can help me.

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 gjf · Dec 04, 2014 at 10:30 AM 0
Share

"I could have coded the timers on each item but that would be not efficient." seems to contradict your question title "How to use the same script on different objects at the same time?".

you've effectively answered it yourself - add the script to each object. job done.

out of purely selfish curiosity, why don't you think it's efficient?

1 Reply

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

Answer by RudyTheDev · Dec 04, 2014 at 02:27 PM

To be efficient, you'd want 1 timer per whatever needs a timer. That leads to a separate timer manager and multiple timers. (There are of course many other ways to do timers, but this is a "typical" general case scenario.) Something like this:

 /// <summary> A timer manager class </summary>
 public class Timers : MonoBehaviour
 {
     /// <summary> List of our active timers </summary>
     private List<Timer> myTimers = new List<Timer>();
 
     public void Update()
     {
         // Go through all our timers
         for (int i = 0; i < myTimers.Count; i++)
         {
             // Update the timer and see if it signals completion
             if (myTimers[i].UpdateStuff())
             {
                 // Do some timer completion thing on whatever data the timer has, in this example, GameObject -- we'll just enable it for example sake
                 myTimers[i].targetObject.SetActive (true);
 
                 // Get rid of the completed timer
                 myTimers.RemoveAt (i);
                 i--; // Since we modify the collection (and we know it's the current index), we need step 1 element back or we will skip the one that is now in position i
             }
         }
     }
 
     /// <summary> Creates and starts a new timer for the specified amount of time for some object </summary>
     public void StartNewTimer (float totalSeconds, GameObject targetGameObject)
     {
         myTimers.Add (new Timer (totalSeconds, targetGameObject));
     }
     
     /// <summary> A self-contained timer class </summary>
     private class Timer
     {
         /// <summary> Some data we are storing in this timer, in this example, a GameObject </summary>
         public readonly GameObject targetObject;
 
         private float totalSeconds;
         private float hours;
         private float minutes;
         private float seconds;
 
         public Timer (float givenTotalSeconds, GameObject newTarget)
         {
             totalSeconds = givenTotalSeconds;
             targetObject = newTarget;
         }
 
         /// <summary> Updates the internal time and returns whether it has completed </summary>
         public bool UpdateStuff ()
         {
             minutes = Mathf.Floor (totalSeconds / 60); //Converts totalSeconds to minutes
             hours = Mathf.Floor (minutes / 60); //Converts totalSeconds to hours
             seconds = totalSeconds - minutes * 60; //Converts totalSeconds to seconds
             totalSeconds -= Time.deltaTime; //deducts one second
             //Debug.Log (hours.ToString () + ":" + minutes.ToString () + ":" + (Mathf.Floor (seconds)).ToString ()); //Displays current time left
 
             // Return true, as in "completed", when the timer runs down to 0
             return totalSeconds <= 0f;
         }
     }
 }

(Have not actually tested this.)

Attach the Timers to some GameObject that you want to be your timer manager. Then call StartNewTimer(time) to start a new timer. There are many ways to define what the timer should do on expiry, you have not given any concrete notes on that, so you may need to store other or extra information per Timer.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Start timer with trigger 1 Answer

Calling methods 0 Answers

Timer doesn't work properly 2 Answers

When Score goes up by 25 change timer value 1 Answer

Countdown Through a Label? 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