- Home /
More timer from same class
Hello. I wrote a CountDownTimer.cs class for displaying the time left on my online multiplayer -game. Now i want to extend it with two more timers like:
private CountDownTimer RoundTimer;
private CountDownTimer SpawnTimer;
private CountDownTimer HideTimer;
If I inicialize them like:
void Start()
{
isGameStarted = false;
RoundTimer = GetComponent<CountDownTimer>();
SpawnTimer = GetComponent<CountDownTimer>();
HideTimer = GetComponent<CountDownTimer>();
}
..then of course, if I try to display them all the timers will be the same, because their source is only on component. My question is:
Do I have to make my CountDownTimer class abstract and make three derived class from it for each timer and put them on a GameObject so I can have them by GetComponent
or make three empty child on my GameLogic GameObject and use the same CountDownTimer on them. So I can have them separately somehow.
or is there a better solution?
Thanks
Answer by DoTA_KAMIKADzE · Apr 09, 2015 at 03:35 PM
I'll suggest you 3 ways to do what you expect, choose whichever you like more:
1)Add a string Name variable to your CountDownTimer.
And then just:
private void Awake()
{
CountDownTimer[] timers = GetComponents<CountDownTimer>();
foreach (CountDownTimer times in timers)
{
switch (times.name)
{
case "RoundTimer":
{
RoundTimer = times;
break;
}
case "SpawnTimer":
{
SpawnTimer = times;
break;
}
case "HideTimer":
{
HideTimer = times;
break;
}
}
}
}
2)[SerializeField].
Just do this:
[SerializeField]
private CountDownTimer RoundTimer;
[SerializeField]
private CountDownTimer SpawnTimer;
[SerializeField]
private CountDownTimer HideTimer;
And then in Inspector throw your different timer scripts on their respective place.
Remember DO NOT use GetComponent now - all values will be set through inspector.
3)If you don't need to add some values in inspector then just:
private CountDownTimer RoundTimer = new CountDownTimer();
private CountDownTimer SpawnTimer = new CountDownTimer();
private CountDownTimer HideTimer = new CountDownTimer();
I think the first method is "ugly". If I'm not wrong the 3rd method can only be used if the script doesn't use $$anonymous$$onobehaviour functions like Start() or Update(). $$anonymous$$ine does so it couldn't work. I used your 2nd method, but combined with my 2nd point on my list. I had to add 3 empty childs because the online functionality didn't work. If i want to use RPC in the CountDownTimer, I hve to use 3 differenet Photon View components, beacuse the RPC's interferenced. Btw the second method worked. Thanks :)
Well I haven't seen your CountDownTimer.cs so I could only guess. $$anonymous$$oreover there are even more ways to do the same thing - those 3 was just probably the easiest ones to implement in their niche. Even more - you can combine them )) but then again that all highly depends on what you exactly do with your timers and what you expect ;)
As for the 3 example if you want them to be added as a component you can always do that:
void Start ()
{
RoundTimer = gameObject.AddComponent<CountDownTimer>();
}
But as I said the third road is only suitable if you don't need to tweak values in Inspector.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Respawn timer? Duplicate instance OnDestroy question... 0 Answers
Call compoent by name 2 Answers
Which timer is more efficient ? 3 Answers