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 /
avatar image
0
Question by mortyc · Mar 05, 2018 at 08:02 AM · programmingfunctionspawningc #asteroids

Problem with my Coroutine

Hi i am creating an asteroid game and for my asteroids i used a Coroutine. Now i am trying to create boss fight after i destroyed 100 asteroids. How can i do that. I tried to stop the loop but it wont turn back again. Also if i set up do to something when is bigger then 100 asteroids the other setups wont stop. Sorry for my English and also for my post i am new to this domain. Thank you very much.

public class RandomHazzards : MonoBehaviour {

 public GameObject Hazzard;
 public GameObject Hazzard2;
 public GameObject Hazzard3;
 public GameObject Hazzard4;
 public GameObject Hazzard5;
 public GameObject Hazzard6;
 public GameObject Hazzard7;
 
   
 public GameObject Spawner;

 public Vector3 SpawnValues;
 public Vector3 SpawnValues2;
 public Vector3 SpawnValues3;
 public Vector3 SpawnValues4;
 public Vector3 SpawnValues5;
 public Vector3 SpawnValues6;
 public Vector3 SpawnValues7;

 public int HazzardCount;


 public float spawnwait;
 public float StartWait;
 public float WaveWait;
 public float IncWave;
 public float Waveplus = 0.2f;


 public int DestroyedAsteroids;



 void Start()
 {

     StartCoroutine (SpawnWaves ());
 

 }
 void Update()
 {
     GameObject theasteroid = GameObject.Find("GameController");
     GameController gamecont = theasteroid.GetComponent<GameController>();
     if (gamecont == null)
     {
         //   Debug.Log("Game COntroller not found");
     }
     if (gamecont != null)
     {
         // Debug.Log("Itworks");
         DestroyedAsteroids = gamecont.asteroidskilled;
     }

     IncWave = WaveWait - Waveplus *Time.deltaTime ; 

     if (IncWave <= 0.5f) {
         IncWave = WaveWait;
     }
 
 }

 

IEnumerator SpawnWaves() {

    yield return new WaitForSeconds(StartWait);

     while (true) {


         for (int i = 0; i < HazzardCount; i++) {

                 Vector3 spawnPosition = new Vector3 (Random.Range (SpawnValues.x, -SpawnValues.x), SpawnValues.y, SpawnValues.z);
                 Vector3 spawnPosition2 = new Vector3 (Random.Range (SpawnValues2.x, -SpawnValues2.x), SpawnValues2.y, SpawnValues2.z);
                 Vector3 spawnPosition3 = new Vector3 (Random.Range (SpawnValues3.x, -SpawnValues3.x), SpawnValues3.y, SpawnValues3.z);
                 Vector3 spawnPosition4 = new Vector3 (Random.Range (SpawnValues4.x, -SpawnValues4.x), SpawnValues4.y, SpawnValues4.z);
                 Vector3 spawnPosition5 = new Vector3 (Random.Range (SpawnValues5.x, -SpawnValues5.x), SpawnValues5.y, SpawnValues5.z);
                 Vector3 spawnPosition6 = new Vector3 (Random.Range (-SpawnValues6.x, SpawnValues6.x), SpawnValues6.y, SpawnValues6.z);
                 Vector3 spawnPosition7 = new Vector3 (Random.Range (-SpawnValues7.x, SpawnValues7.x), SpawnValues7.y, SpawnValues7.z);
         
                 Quaternion SpawnRotation = Quaternion.identity;


                 if (DestroyedAsteroids >= 0) {
                     Instantiate (Hazzard, spawnPosition, SpawnRotation);
                     Instantiate (Hazzard2, spawnPosition2, SpawnRotation);
                     Instantiate (Hazzard3, spawnPosition3, SpawnRotation);
                     Instantiate (Hazzard4, spawnPosition4, SpawnRotation);
                     Instantiate (Hazzard5, spawnPosition5, SpawnRotation);
                 }

                 if (DestroyedAsteroids >= 20) {


                     Instantiate (Hazzard6, spawnPosition6, SpawnRotation);

                 }

                 if (DestroyedAsteroids >= 40) {
                     Instantiate (Hazzard7, spawnPosition7, SpawnRotation);
                 }
                 if (DestroyedAsteroids >= 70) {
                     Instantiate (Hazzard6, spawnPosition6, SpawnRotation);
                     Instantiate (Hazzard7, spawnPosition7, SpawnRotation);
                 }
                 if (DestroyedAsteroids >= 90) {
                     Instantiate (Hazzard7, spawnPosition7, SpawnRotation);

                 }

                 yield return new WaitForSeconds (IncWave);

             }
         }
     }
 }
 
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 Greg_lrgg · Mar 05, 2018 at 09:22 AM

Hey,

First off you might want to use Arrays to store your hazards and spawnpos, it 'll make your code clearer and more attractive to look at. Believe me this might be "secondary" to you but having a good looking code is better both for you and people who are trying to help you.
Secondly, if you have to type the same instructions more than a few times (namely 2 or 3) it means that something is either wrong or poorly optimised in your code. You can declare a function that spawns several of your hazards at several spwan position.
Now to answer your question you should not do the test inside of your coroutine, which is what you are doing if I read correctly. Do it in your main code, and create a dedicated instructions to spawn your boss.
Lastly to be able to start and restart a coroutine, you should do the following:
First of all declare a variable IEnumerator myCoroutine which will hold your actual coroutine. When you stop a coroutine, it actually does not "exist" anymore to make it simple, so you should have a line that says to Unity "Hey this is the coroutine I want to use" whenever you want to start it again.
Namely: myCoroutine = MyCoroutine(), where MyCoroutine is your implemented coroutine. This way Unity will have a correct reference your coroutine when you will say:

StartCoroutine( myCoroutine)

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

137 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

Related Questions

Can somebody tell me which functions I would need to use to make this pseudocode work? 1 Answer

[Unity] Cómo agrego la salida y la entrada en el laberinto Unity? 0 Answers

Unity IAP Disable Ads 0 Answers

Invoking error handler due to uncaught exception: abort(-1) at jsStackTrace (NAME.wasm.framework.unityweb:1559:13) 0 Answers

adding two integers together once every half second,adding two integers together with delay? 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