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 /
avatar image
0
Question by Silvermurk · May 15, 2016 at 08:58 PM · coroutinetimespawnienumerator

spawn timer problem

Somehow i can`t manage to make this enemys spawn with timer. THey do spawn as soon as i kill one, but it happens INSTANTLY, despire WaitForSeconds being in script. Need an idea how to make new guy appear like 2 seconds after you kill the one before him, not instantly. Any help would be appritiated.

 using UnityEngine;
 using System.Collections;
 
 public class SpawnEnemy : MonoBehaviour
 {
 
     public GameObject[] EnemyTypes;
     ScoreManager scoreManager;
 
     void Awake()
     {
         scoreManager = GameObject.Find("GameManager").GetComponent<ScoreManager>();
     }
 
     void Update()
     {
         if (scoreManager.inPlay)
         {
             if (scoreManager.EnemyCount <= 3)
                 Spawn();
         }
     }
 
     void Spawn()
     {
         Debug.Log("SPAWN!");
         Vector3 spawnV = new Vector3(Random.Range(-6, 6), Random.Range(-3, 3), 0);
         int s = Random.Range(0, EnemyTypes.Length);
         Instantiate(EnemyTypes[s], spawnV, Quaternion.identity);
         //Instantiate(EnemyTypes[0], Vector3.zero, Quaternion.identity);
         scoreManager.EnemyCount++;
         StartCoroutine("Wait");
     }
     IEnumerator Wait()
     {
         yield return new WaitForSeconds(2);
     }
 }
 
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 JoshDangIt · May 16, 2016 at 01:50 AM

You aren't using the co-routine properly. Try this instead (Untested):

 using UnityEngine;
 using System.Collections;
 
 public class SpawnEnemy : MonoBehaviour {
 
     public GameObject[] EnemyTypes;
     public float spawnRate = 2f; //probably don't want to hardcode that
     ScoreManager scoreManager;
 
     float timeSinceLastSpawn = 0f;
     
     void Awake()
     {
         scoreManager = GameObject.Find("GameManager").GetComponent<ScoreManager>();
     }
     
     void Update()
     {
         timeSinceLastSpawn += Time.deltaTime;
         if (scoreManager.inPlay)
         {
             if (scoreManager.EnemyCount <= 3 && timeSinceLastSpawn >= spawnRate)
                 Spawn();
         }
     }
     
     void Spawn()
     {
         Debug.Log("SPAWN!");
         Vector3 spawnV = new Vector3(Random.Range(-6, 6), Random.Range(-3, 3), 0);
         int s = Random.Range(0, EnemyTypes.Length);
         Instantiate(EnemyTypes[s], spawnV, Quaternion.identity);
         //Instantiate(EnemyTypes[0], Vector3.zero, Quaternion.identity);
         scoreManager.EnemyCount++;
         timeSinceLastSpawn = 0f;
     }
 
 }
Comment
Add comment · Show 3 · 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 Silvermurk · May 16, 2016 at 11:34 AM 0
Share

Works like a charm, thanks. $$anonymous$$ind ic i`ll ask for your assistance again? Can you explain, why my code(before editing) used to spawn max amount of enemys per spawn cycle? $$anonymous$$ean if I set it to <=3 I produced 3 new ones even if only 1 was killed)

avatar image JoshDangIt Silvermurk · May 16, 2016 at 01:17 PM 0
Share

I'll try but it's kind of hard to explain.

When you used StartCoroutine(), nothing was stopping the Update function from continuing to be called. Coroutines run along side all the other functions, so your wait function didn't actually wait for anything.

avatar image Silvermurk JoshDangIt · May 16, 2016 at 03:52 PM 0
Share

Will need to get along with corutines. $$anonymous$$ind of strange how they work sometimes) : Thanks again.

avatar image
1

Answer by 5c4r3cr0w · May 16, 2016 at 06:52 AM

@Silvermurk

Replace Spawn() in Update() to your wait coroutine like :

      void Update()
      {
          timeSinceLastSpawn += Time.deltaTime;
          if (scoreManager.inPlay)
          {
              if (scoreManager.EnemyCount <= 3 && timeSinceLastSpawn >= spawnRate)
                 StartCoroutine(Wait());
          }

And then call Spawn function in that coroutine like :

 IEnumerator Wait()
      {
          yield return new WaitForSeconds(2);
          Spawn();
      }

This will pretty much do your job. Good luck.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

how do i make my spawn system spawn every 2 seconds then wait 10minutes? 3 Answers

How to have IEnumerators run but not in Update 2 Answers

Spawn objects in burst of three? 1 Answer

Returning an IEnumerator as an int? 1 Answer

OnCollisionEnter as IEnumerator problem (WaitForSecond) 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