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 ragefordragons · Dec 11, 2017 at 11:47 PM · editorfunctionloopfreezing

Unity Keeps Freezing on Play (Again!)

Okay so Im making a bullet hell style shooter and i have a code that spawns enemies here. Basically it has a number of enemeies to spawn, then once that number runs out it moves onto the next wave. It works spawing the first few enemies, but once the count runs out the editor crashes. I know this is becuase of an infinite loop, (Ive had this issue before,) and the "continue" and "yeild return null" parts fixed it before. However, it must be different for have a IEnumerator. Any help on how to fix it?

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class MonsterSpawner : MonoBehaviour {

 public float delta = 1.5f;
 public float speed = 2.0f;
 private Vector3 startPos;
 public Transform spawnpoint;
 public GameObject shell;
 public GameObject redshell;
 public int EnemySpeed = 7;
 public int enemiesleft = 10;
 public int enemiesnext = 15;
 public int wave = 1;

 public bool useInvoke;
 public bool running;

 // Use this for initialization
 void Start () {
     startPos = transform.position;

     

     

     if (useInvoke)
         InvokeRepeating("Spawn1", 0, 3);
     else
         StartCoroutine(shellspawn1());
 }
 
 // Update is called once per frame
 void Update () {

     if (enemiesleft < 1)
     {
         wave = wave + 1;
         if (wave == 1)
         {
             enemiesnext = 15;
         } else if (wave == 2)
         {
             enemiesnext = 1;
         }

         enemiesleft = enemiesnext;
     }

     Vector3 v = startPos;
     v.x += delta * Mathf.Sin(Time.time * speed);
     transform.position = v;
 }

 void spawnshell()
 {
     var shellspawn = (GameObject)Instantiate(
         shell,
         spawnpoint.position,
         spawnpoint.rotation);

     shellspawn.GetComponent<Rigidbody>().velocity = shellspawn.transform.up * EnemySpeed;

     Destroy(shellspawn, 10.0f);
 }

 IEnumerator shellspawn1()
 {
     running = true;

     while (running)
     {
         if (enemiesleft > 1)
         {
         enemiesleft = enemiesleft - 1;

         if (wave == 1)
             {
             spawnshell();
              yield return new WaitForSeconds(0f + (Random.Range(1.5f, 3f)));
              continue;

             } else if (wave == 2)
             {
                 spawnredshell();
                 yield return new WaitForSeconds(0f + (Random.Range(1.5f, 3f)));
                 continue;
             }
         }


     }
     yield return null;
 }
 void Spawn1()
 {
     if (wave == 1)
     {
         spawnshell();
         

     }
     else if (wave == 2)
     {
         spawnredshell();
        
     }
 }

 void spawnredshell()
 {
     var shellspawnred = (GameObject)Instantiate(
                redshell,
                spawnpoint.position,
                spawnpoint.rotation);

     shellspawnred.GetComponent<Rigidbody>().velocity = shellspawnred.transform.up * EnemySpeed;

     Destroy(shellspawnred, 10.0f);
 }

}

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

Answer by bobisgod234 · Dec 12, 2017 at 12:33 AM

If "enemiesleft" is less then or equal to 1, then you end up with an infinite loop in your "shellspawn1", as the yield statements in that while loop can only be reached if enemiesleft is greater than 1.

Try adding a yield reutrn null at the very end of your while loop:

  IEnumerator shellspawn1()
  {
      running = true;
      while (running)
      {
          if (enemiesleft > 1)
          {
          enemiesleft = enemiesleft - 1;
          if (wave == 1)
              {
              spawnshell();
               yield return new WaitForSeconds(0f + (Random.Range(1.5f, 3f)));
               continue;
              } else if (wave == 2)
              {
                  spawnredshell();
                  yield return new WaitForSeconds(0f + (Random.Range(1.5f, 3f)));
                  continue;
              }
          }
          yield return null;
      }
      yield return null;
  }
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 ragefordragons · Dec 13, 2017 at 02:20 AM 0
Share

Yup, that worked. Thanks a ton!

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

96 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

Related Questions

Question regarding Async loading waiting for a loop to complete. 0 Answers

Is there any way to call OnLevelLoaded when loading a scene in editor? 2 Answers

Can anyone manage to slice at 8192x4096 texture into 64x64 sprites 2 Answers

Call a function from within itself 3 Answers

How to make incorporate a function and a loop 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