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 RandomUser123 · Aug 13, 2014 at 06:32 PM · updatespawn

Trouble with InvokeRepeating and Update

I'm trying to set up a gameplay feature in which enemies will increase in number depending on your score. This is what I have so far:

 void Start()
     {
         //The point in which the new prefab will be spawned
         GameObject[] objs = GameObject.FindGameObjectsWithTag("SpawnPoints") as GameObject[] ;
         spawnPoints = new Vector3[objs.Length] ;
         
         for(int i = 0 ; i < objs.Length ; i++)
         {
             spawnPoints[i] = objs[i].transform.position ;
         }
         endOfArray = spawnPoints.Length;
         InvokeRepeating ("Spawn", timeToBegin, spawnTime);
     }
     
     void Update()
     {
         if(accelShotHS.score == 5)
         {
             spawnEnemyWaveOne ();
         }
 
         else if(accelShotHS.score == 15)
         {
             spawnEnemyWaveTwo ();
         }
 
                 //etc etc...
     }
 
         void Spawn()
     {
         Instantiate(spawnableObject,spawnPoints[Random.Range(0,endOfArray)],Quaternion.identity);
     }
 
     void spawnEnemyWaveOne()
     {
         if(hasSpawned == false)
         {
             CancelInvoke ();
             spawnTime = 4.0f;
             InvokeRepeating ("Spawn", timeToBegin, spawnTime);
             Debug.Log ("Wave 1");
             hasSpawned = true;
         }
     }
 
     void spawnEnemyWaveTwo()
     {
         if(hasSpawned == false)
         {
             CancelInvoke ();
             spawnTime = 3.0f;
             InvokeRepeating ("Spawn", timeToBegin, spawnTime);
             Debug.Log ("Wave 2");
             hasSpawned = true;
         }
     }

So I want the enemies to increase depending on the score and I have a Debug.Log() in place that tells me which wave it's in, it starts off fine, it goes to wave one, but it never leaves that wave. When i get passed 15, it stays in the same wave.

Anyone know why? Thanks in advance

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

3 Replies

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

Answer by robertbu · Aug 13, 2014 at 06:59 PM

You have a sure problem, and a couple of other things that are of concern. First this logic:

     if(accelShotHS.score == 5)
     {
         spawnEnemyWaveOne ();
     }
 
     else if(accelShotHS.score == 15)
     {
         spawnEnemyWaveTwo ();
     }

We don't see if 'accelShotHS.score' ever reaches 15, but if it does, this code still would not work. When the value is 15, this line:

      if(accelShotHS.score == 5)

...is still true. That means the 'else' clause would never get executed. Reverse the logic so you check for 15 first:

     if(accelShotHS.score == 15)
     {
         spawnEnemyWaveOne ();
     }
 
     else if(accelShotHS.score == 5)
     {
         spawnEnemyWaveTwo ();
     }

The first potential issue is that I see no code here that sets 'hasSpawned' to false, and you need it to be false for line 49 to be true and the second wave to begin.

The second potential issue (and I'm less sure of this one), but I believe there can be problems if you do a CancleInvoke() and an immediate InvokeRepeating(). If this is the case (or even if not), rewrite your InvokeRepeating() as a CoRoutine(). Replace your Spawn() method with:

    IEnumerater Spawner()    {
         yield return new WaitForSeconds(startWait(timeToBegin);
         while (true) {
               Instantiate(spawnableObject,spawnPoints[Random.Range(0,endOfArray)],Quaternion.identity);
               yield return new WaitForSeconds(spawnTime);
         }
    }


Replace the InvokeRepeating() in line 12 with:

   StartCoroutine(Spawner());

Then changing 'spawnTime' will change the rate of spawning without stopping or starting anything.

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 RandomUser123 · Aug 13, 2014 at 07:11 PM 0
Share

Thanks for the help robertu, seems I'm doing a lot wrong :/ This works great, best get learning.

avatar image
1

Answer by Kiwasi · Aug 13, 2014 at 07:18 PM

You could write this all in a coroutine. This format eliminates both your Update method and your SpawnWaveXXX methods

 void Start (){
     // Other stuff
     StartCoroutine(SpawnTimer());
 }
 
 private IEnumerator SpawnTimer(){
     while (accelShotHS.score <6){
         Spawn();
         yield return new WaitForSeconds(5);
     }
     while (accelShotHS.score <16){
         Spawn();
         yield return new WaitForSeconds(4);
     }
     while (true){
         Spawn();
         yield return new WaitForSeconds(3);
     }
 }

An alternate way to code the coroutine would be. With this code there is no need to stop the coroutine once in starts, just adjust spawnTime and the code will take care of the adjustment automatically.

 private IEnumerator SpawnTimer(){
     while (true){
         Spawn();
         yield return new WaitForSeconds(spawnTime);
     }
 }


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
avatar image
0

Answer by mediumal · Aug 13, 2014 at 07:01 PM

I think it is because hasSpawned is never set to false again.

Also, you should protect against the case where you manage to score two points in one frame, causing your score to jump from 14 to 16 without ever hitting 15.

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

23 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

Related Questions

Spawning different objects 1 Answer

Convert InvokeRepeating into the Update method... 1 Answer

How to make a spawner wait for x seconds? 2 Answers

spawn every frame, and i want it to spawn only once. 1 Answer

How should i add raycast to multiple game objects??? 0 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