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

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();