Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Mar 29, 2019 at 06:37 PM by NoctisShadowzel for the following reason:

The question is answered, right answer was accepted.

avatar image
0
Question by NoctisShadowzel · Mar 28, 2019 at 08:53 PM · scripting problemcoroutinesspawner

I want to make a Monster Spawner that spawns a monster in every x seconds (in real time); but I can't get timer to work properly...

I'm trying to make a Spawner, which will Instantiate a prefab in every x seconds. I tried many times, but I can't get coroutines to work properly. This is the last form of code; which waits x seconds before spawning any prefab; after that, summons all monsters immediately. I don't know what am I missing; any help will be welcomed!
TY for taking time!


 using System.Collections;
 using UnityEngine;
 
 public class MonsterSpawner : MonoBehaviour
 {
     public GameObject[] spawnPoints;
     
     public int monstersCount;
 
     public GameObject monster2;
     private GameObject monsterSprite;
 
     public float spawnDelay = 5f;
 
     void FixedUpdate()
     {
         if (monstersCount < 1)
             Destroy(gameObject);
 
         StartCoroutine(SpawnMonster());
     }
 
     IEnumerator SpawnMonster()
     {
         yield return new WaitForSecondsRealtime(spawnDelay);
 
         int pointSelected = Random.Range(0, 3); //To use as index for reaching array of spawn points.
         Vector2 pointToSpawn = spawnPoints[pointSelected].transform.position;
 
         GameObject clonePrefab = Instantiate(monster2, pointToSpawn, Quaternion.identity);
 
         monsterSprite = clonePrefab.transform.GetChild(0).gameObject;
         if (pointSelected == 1)
             monsterSprite.GetComponent<SpriteRenderer>().flipX = true;
         else
             monsterSprite.GetComponent<SpriteRenderer>().flipX = false;
 
         monstersCount--;
     }
 }


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

  • Sort: 
avatar image
1
Best Answer

Answer by WarmedxMints · Mar 28, 2019 at 09:29 PM

The problem is you are starting a new co-routine every physics tick. I'm not sure why you are using fixed update. You could use a while statement in the coroutine and just start it once. Like so;

 using System.Collections;
 using UnityEngine;
 
 public class MonsterSpawner : MonoBehaviour
 {
     //You may as well use transform here rather than gameobjects
     public Transform[] SpawnPoints;
 
     public int MonstersCount;
 
     public GameObject Monster2;
 
     [SerializeField]
     private float _spawnDelay = 5f;
 
     private WaitForSecondsRealtime _waitTime;
 
     private void Start()
     {
         _waitTime = new WaitForSecondsRealtime(_spawnDelay);
 
         StartCoroutine(SpawnMonster());
     }
 
     IEnumerator SpawnMonster()
     {
         while (MonstersCount >= 1)
         {
             var pointSelected = Random.Range(0, 3); //To use as index for reaching array of spawn points.
             var pointToSpawn = SpawnPoints[pointSelected].position;
 
             var clonePrefab = Instantiate(Monster2, pointToSpawn, Quaternion.identity);
 
             var monsterSprite = clonePrefab.transform.GetChild(0).gameObject.GetComponent<SpriteRenderer>();
 
             if (monsterSprite != null)
             {
                 monsterSprite.flipX = pointSelected == 1;
             }
 
             MonstersCount--;
 
             yield return _waitTime;
         }
 
         Destroy(gameObject);
     }
 }

You could also use a timer in the update method and do away with the coroutine if you wish or create a method you could call to spawn x monsters whenever you wish rather than destroying the spawner.

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 NoctisShadowzel · Mar 29, 2019 at 05:25 PM 0
Share

Thank you for the answer! I don't have much practice in C#, it takes a while to get best solution. I was using FixedUpdate to prevent frames from having different life-times, so there will be no frame-delays because of a frame that needs more time to complete calculations it needs to do.

I was destroying the Spawner, because even if monstersCount is <1, Update will keep checking if state. I wanted to prevent unnecessary calculations...

And also thanks for maintaining my code, I will consider your code while re-writing my script.

Cheers ^^

Follow this Question

Answers Answers and Comments

242 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 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 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 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 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 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 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 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 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 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 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 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 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

Delay in Editor Script 0 Answers

SteamVR menu button stops coroutines when pressed. 2 Answers

How to keep program linear and synchronous while getting user input? 1 Answer

Unity 5.6 - Get list of animations 1 Answer

Same script working yesterday not working today 2 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