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 hbalint1 · Apr 09, 2015 at 03:03 PM · c#timercomponentscustom class

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

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
0
Best Answer

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

Comment
Add comment · Show 2 · 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 hbalint1 · Apr 09, 2015 at 04:31 PM 0
Share

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 :)

avatar image DoTA_KAMIKADzE · Apr 09, 2015 at 04:52 PM 0
Share

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

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

19 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

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


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