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 tgfogt1418 · Nov 28, 2020 at 04:31 PM · instantiatespawning-enemiesslowdown

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

My game contains about 500 trees (my own preFabs, not Unity trees) each of which Instantiate()s an enemy every few minutes or so. After about 40 enemies have been spawned, the game slows considerably, and gets worse from there. This isn't to do with the enemies, btw-- if I simply place several few hundred enemies into the scene everything works fine, so it must be this script.

This is the script on the tree. As you can see, it's very sparse, not sure what's slowing the game down. I also don't know why the number of enemies would make this script slower, since it doesn't store them... Thoughts?

 public GameObject creature;
 public Vector3 displacement = new Vector3(0,0,0);
 public float spawnPeriod = 300.0f;
 public float firstSpawnPeriod;
 bool readyToSpawn = true;
 bool first;
 
 void Start()
 {
     first = true;
     firstSpawnPeriod = Random.Range(2.0f, spawnPeriod);
 }

 void Update () 
 {
     if (readyToSpawn) 
     {
         Spawn();
         StartCoroutine(Delay());
     }
 }
 
 IEnumerator Delay() 
 {
     if (first)
     {
         readyToSpawn = false;
         yield return new WaitForSeconds(firstSpawnPeriod);
         readyToSpawn = true;
         first = false;    
     }
     else
     {
         readyToSpawn = false;
         yield return new WaitForSeconds(spawnPeriod);
         readyToSpawn = true;
     }
 }
 
 void Spawn()
 {
     Instantiate(creature, transform.position+displacement, transform.rotation);
 }
Comment
Add comment · Show 1
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 tgfogt1418 · Nov 29, 2020 at 08:30 AM 0
Share

[SOLVED] turns out the movement script on the enemies was in fact the problem. The script loops over other gameobjects in the scene, which is why it ran with no problem when I didn't have trees in the scene. I've changed that code and it now runs smoothly with up to 200 enemies. I'll create a pool as suggested so that this number isn't exceeded. Thanks for the help!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by highpockets · Nov 28, 2020 at 04:38 PM

If this script is on 40 trees and every 6 minutes you instantiate 40 prefabs at the same time you likely notice the major hit when the instantiation happens. Destroying and instantiating takes a fair amount of resources of the CPU up. You might want to look at object pooling to cache/reuse the enemies when you need them instead of instantiating and destroying every time.

Comment
Add comment · Show 5 · 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 stephen_gregory · Nov 28, 2020 at 05:03 PM 0
Share

Object pooling design pattern is the best answer!

avatar image tgfogt1418 · Nov 28, 2020 at 05:24 PM 0
Share

Thanks for the reply! I don't think this is the issue, since I specifically set the trees to NOT spawn all at once by initializing a random first spawn time on line 11: firstSpawnPeriod = Random.Range(2.0f, spawnPeriod)

The slowdown is also not once every 6 $$anonymous$$utes, but rather a gradual increase. I looked into pooling and will probably implement it if I can't find the root of the issue, though it seems to me pooling would be helpful to avoid the instantaneous slowdown of an Instantiate() call, which is not what I'm experiencing :/

avatar image Rorrors tgfogt1418 · Nov 28, 2020 at 06:27 PM 1
Share

I would also go with the object pooling suggestion. But its usually handy to find the problem first. And that you can see in the profiler. Go to Windows, Analysis, Profiler. And then look at the peaks and do a "Deep Profile" at that point, so you can see all relevant information. Here you can see the steps that you can take; https://www.youtube.com/watch?v=fROTtgZK-Zs

avatar image tgfogt1418 Rorrors · Nov 29, 2020 at 08:30 AM 0
Share

Profiler did the trick :) Thanks for the tip!

avatar image highpockets tgfogt1418 · Nov 28, 2020 at 10:21 PM 0
Share

Sorry, I missed that Random.Range line, but my answer would have been similar besides the same spawn time. Definitely a good idea to take a look at the profiler as there could be many other factors

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

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

Instantiate Slowdown? 1 Answer

Checking if object intersects? 1 Answer

In a closed room Instantiate game objects around the player randomly. 1 Answer

Spawning prefabs dependant upon Health UI? 2 Answers

How to spawn objects but set a cap of how many can be alive at the same time? 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