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 profanicus · Jan 02, 2012 at 03:17 AM · timertimers

What is a good method for tracking an array of different times?

Hello, I am trying to implement a spawner object and am looking for some advice about the best approach in dealing with multiple timers. Here is a stripped down example of what I am doing, in c#:

 [System.Serializable]
 public class SpawnerList
 {
     public Transform objectToSpawn;
     public float secondsUntilNextSpawn = 1;
 }
 
 public class Spawner : MonoBehaviour
 {
     public SpawnerList[] spawnerList;
 }

So in the inspector you have an array of SpawnerList objects, each having a prefab and a spawn time. What would be the best approach to dealing with tracking those different times, and spawning the different prefabs at the right times?

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

2 Replies

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

Answer by Bunny83 · Jan 02, 2012 at 03:17 AM

If each "SpawnerList"-item (which should have a singular name like SpawnItem) should be spawned independently, just start a seperate coroutine for each item.

edit: changed my example

[System.Serializable] public class SpawnItem { public Transform objectToSpawn; public float secondsUntilNextSpawn = 1; }

public class Spawner : MonoBehaviour { public SpawnItem[] spawnList; IEnumerator SpawnLoop(SpawnItem Item) { while(true) // spawns infinite { Instantiate(Item.objectToSpawn); yield return new WaitForSeconds(Item.secondsUntilNextSpawn); } } void Start() { foreach (SpawnItem I in spawnList) StartCoroutine(SpawnLoop(I)); } }

Or which is propably much simpler, attach as many scripts as needed to a GameObject. Since every item works on it's own just create a MonoBehaviour which handles a single object and just attach as many you need:

public class Spawner : MonoBehaviour
{
    public Transform objectToSpawn;
    public float secondsUntilNextSpawn = 1;
    IEnumerator Start()
    {
        while(true)   // spawns infinite
        {
            Instantiate(objectToSpawn);
            yield return new WaitForSeconds(secondsUntilNextSpawn);
        }
    }
    // If you want to stop the spawn loop when the script get disabled, just add this:
    void OnDisable()
    {
        StopAllCoroutines();
    }
}
Comment
Add comment · Show 1 · 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 profanicus · Jan 02, 2012 at 06:05 AM 0
Share

Thank you, I like this as it has increased my understanding of coroutines! For some reason I thought calling the same method as a coroutine multiple times would result in only one coroutine. Now I know better.

avatar image
2

Answer by Rod-Green · Jan 02, 2012 at 04:09 AM

Start a coroutine inside each object...

 [System.Serializable]
 public class SpawnerItem
 {
     public Transform objectToSpawn;
     public float m_spawnDelay = 1;
     Spawner m_spawner;
     bool m_canRun;
     public void Start(Spawner p_spawner)
     {
         m_spawner = p_spawner;
         m_canRun = true;
         p_spawner.StartCoroutine(SelfRun());
     }
     
     public void Stop()
     {
         m_canRun = false;
     }
     
     IEnumerator SelfRun()
     {
         while(m_canRun)
         {
             yield return new WaitForSeconds(m_spawnDelay);
             Debug.Log("Im running every..." + m_spawnDelay);
         }
     }
 }
 
 public class Spawner : MonoBehaviour
 {
     public SpawnerItem[] m_spawnerItems;
     
     void Start()
     {
         if(m_spawnerItems == null)
             return;
         foreach(SpawnerItem eachItem in m_spawnerItems)
         {
             if(eachItem == null)
                 continue;
             eachItem.Start(this);
         }
     }
 
 }
Comment
Add comment · Show 4 · 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 Bunny83 · Jan 02, 2012 at 04:17 AM 0
Share

This would work when you pass-in a $$anonymous$$onoBehaviour instance, because StartCoroutine is a member function of $$anonymous$$onoBehaviour.

avatar image Rod-Green · Jan 02, 2012 at 04:23 AM 0
Share

@Bunny83 oh of course.. :S silly me O$$anonymous$$ tweaked a little..

avatar image profanicus · Jan 02, 2012 at 07:12 AM 0
Share

Thanks, this is very interesting. To my untrained eye it seems a more "correct" solution to have the SpawnerItem deal with its coroutine as you demonstrate here. I used Bunny83's as it fit more easily into what I had, so I guess I have to mark that as correct. But this is good too, thank you.

avatar image Rod-Green · Jan 02, 2012 at 07:32 AM 0
Share

Hey no problem whatever works. Goodluck

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to add a delay to a bomb explostion 1 Answer

How to make reverse countdown timer in unity? 1 Answer

jump timer 1 Answer

Increase as timer decreases 1 Answer

Are there any performance differences between a timer and using Invoke? 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