- Home /
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.
"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?
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
.
Your answer
Follow this Question
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