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 kayo-chan · Oct 15, 2019 at 01:45 AM · spawning-enemies

How can I increase the rate of spawning with respect to scores gotten?

Here is my code, I tried doing the following but it doe not work.

 using UnityEngine;
 
 public class EnemyManager : MonoBehaviour
 {
     public PlayerHealth playerHealth;
     public GameObject enemy;
     public float spawnTime =10f;
     public Transform[] spawnPoints;
 
 
     void Start()
     {
             InvokeRepeating("Spawn", spawnTime, spawnTime);
 
     }
     void Update()
     {
          if (ScoreManager.score >= 100 & ScoreManager.score < 200)
         
             InvokeRepeating("Spawn", 3, 7);
             enabled = false;
         
         if (ScoreManager.score >= 200 & ScoreManager.score < 300)
         
             InvokeRepeating("Spawn", 3, 4);
             enabled = false;
 
     
         if (ScoreManager.score >= 300)
             InvokeRepeating("Spawn", 1, 1);
             enabled = false;
     }
 
        void Spawn ()
     {
         if(playerHealth.currentHealth <= 0f)
         {
             return;
         }
 
         int spawnPointIndex = Random.Range (0, spawnPoints.Length);
 
         Instantiate (enemy, spawnPoints[spawnPointIndex].position,spawnPoints[spawnPointIndex].rotation);
     }
 }
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

Answer by GrayLightGames · Oct 15, 2019 at 04:16 AM

You may need to call CancelInvoke to stop the original InvokeRepeating, but I don't have much experience with InvokeRepeating. I believe the original InvokeRepeating will continue until you cancel it, so you'll have multiple InvokeRepeatings that overlap. If someone with more experience can confirm this, that would be great.

If that doesn't work, you could manage the countdown yourself instead of using InvokeRepeating and you'll be able to call Spawn from Update. I'm doing some timer management in my project and it works like a champ. Hope that helps! Here's a little code to illustrate:

 public float spawnTimerMax;
 public float spawnTimer;
 
 void Start()
 {
   spawnTimerMax = 10f;
   spawnTimer = spawnTimerMax;
 }
 
 void Update()
 {
   spawnTimer -= Time.deltaTime;
 
   if(score > 100 && spawnTimerMax > 7f)
   { spawnTimerMax = 7f; }
 
   //other score checking here
 
   //Check timer and spawn
   if(spawnTimer <= 0f)
   {
     Spawn();
     spawnTimer = spawnTimerMax;
   }
 }
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

111 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

Related Questions

Trying to make a spawning enemy code 1 Answer

Spawn multiple enemies in relation to gameobject [SOLVED] 1 Answer

Spawning Enemy spawns more then MaximumEnemy? 1 Answer

SpawnEnemies script slows the game significantly after just 40 or so? 1 Answer

Spawn Point 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