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 WobblyBob · Oct 24, 2016 at 09:22 AM · instantiate prefabefficiency

How can efficiently spawn many (as in, thousands of) objects?

Hello, I want to create a random 2D level at the start of the game. I made an empty game object and attached a script to it that spawns a given number of a certain object in a given range. Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class StarPlacer : MonoBehaviour {
 
     public GameObject[] Stars;
     public int Star_Count;
     public int Star_Space_Min;
     public int Star_Space_Max;
     public GameObject star;
     public string StarTag;
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         Stars = GameObject.FindGameObjectsWithTag(StarTag);
         if (Stars.Length <= Star_Count)
         {
             GameObject BackGround_Star = Instantiate(star) as GameObject;
             BackGround_Star.transform.position = new Vector3(Random.Range(Star_Space_Min, Star_Space_Max), Random.Range(Star_Space_Min, Star_Space_Max), 0.1f);
 
         }
 
     }
 }
 

The problem with this code is that it spawns one object per frame. I'm trying to spawn 2400 objects. It doesn't help that my FPS dies when the game starts so I end up spawning 2400 objects at 3 objects per second!

How can I spawn these objects more efficiently?

EDIT: When the code speaks about stars, it really means any object that I want it to mass spawn.

Comment
Add comment · Show 9
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 doublemax · Oct 24, 2016 at 09:29 AM 0
Share

Do you really need individual gameobjects? For a star background a particle system is usually sufficient.

avatar image WobblyBob doublemax · Oct 24, 2016 at 10:07 AM 0
Share

I've not considered a particle system. However, I don't only spawn stars, I also spawn asteroids, enemies etc. so I don't think a particle system will help. Thanks any way :)

avatar image doublemax WobblyBob · Oct 24, 2016 at 10:13 AM 1
Share

But you'll probably have much fewer asteroids and enemies than stars. $$anonymous$$aybe you can use a particle system for the stars and spawn all other objects the conventional way.

avatar image saschandroid · Oct 24, 2016 at 10:58 AM 1
Share

If you really want to spawn so many objects:

Why are you instantiating the gameobjects in Update() and not in a separate function in/or called by Start()? ... You could just make a for-loop that spawns 2400 objects.

You are trying to find (and so to count) all your instantiated stars every frame by their tag (ins$$anonymous$$d of using a simple counter).

avatar image WobblyBob saschandroid · Oct 24, 2016 at 01:23 PM 0
Share

Thanks for the reply. So you're saying I should make the engine spawn all the objects in one frame. Ok, cool, I'll try that. Thanks.

avatar image WobblyBob · Oct 27, 2016 at 09:49 AM 0
Share

I've tried to move some things around in my code. Now I'm using a while loop. But for some reason, my game freezes and stops doing anything when I test it in the editor. Even when only spawning a couple of objects.

What is wrong with this code?

public GameObject[] Stars; public int Star_Count; public int Star_Space_$$anonymous$$in; public int Star_Space_$$anonymous$$ax; public GameObject star; public string StarTag; // Use this for initialization void Start () { while (Stars.Length < Star_Count) { GameObject BackGround_Star = Instantiate(star) as GameObject; BackGround_Star.transform.position = new Vector3(Random.Range(Star_Space_$$anonymous$$in, Star_Space_$$anonymous$$ax), Random.Range(Star_Space_$$anonymous$$in, Star_Space_$$anonymous$$ax), 0.1f); } }

avatar image saschandroid WobblyBob · Oct 27, 2016 at 11:39 AM 0
Share

You are not adding the instantiated gameobject to your Star-array (or counting them in any way), so Stars.Length will always be 0 and the while-loop is looping forever.

avatar image hexagonius WobblyBob · Oct 27, 2016 at 10:05 PM 1
Share

a for loop is by the way most likely the better choice, because it's definitely going to end. Especially when know how many iterations you want (2400?)

avatar image WobblyBob WobblyBob · Nov 01, 2016 at 12:30 PM 0
Share

Ok, thanks for all the answers. I used a particle system for my background stars and adjusted my object placer script to use a for loop in the start function. All my issues seem to be fixed. Thanks everyone XD.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ghostravenstorm · May 08, 2020 at 12:25 AM

I too am wondering this. In my example, I'm spawning markers at each point on Unity's Cartesian grid that label what that point in space is on the X and Z axis.

I've offset this work into a coroutine, but going into the thousands does really hurt computation.

    public GameObject meterMarkerPrefab;
    GameObject[] listOfMarkers;
    Coroutine generateMeterMarkers;
    IEnumerator GenerateMeterMarkers (int xRange, int zRange, Action onFinished) {
 
       /// xRange is how far from the origin in both directions on the X plane.
       /// zRange is how far from the origin in both directions on the Z plane.
       /// e.g. xRange of 3 is a distance of 7 counting from -3 to +3 inclusive.
 
       int numOfXMarkers = (xRange * 2) + 1;
       int numOfZMarkers = (zRange * 2) + 1;
       int totalMarkers = numOfXMarkers * numOfZMarkers;
 
       listOfMarkers = new GameObject[totalMarkers];
       int markerCount = 0;
       for (int x = xRange * -1; x <= xRange; x++) {
          for (int z = zRange * -1; z <= zRange; z++) {
             listOfMarkers[markerCount++] = GameObject.Instantiate(
                meterMarkerPrefab, new Vector3(x, -0.4f, z), Quaternion.identity
             );
             yield return null;
          }
       }
 
       if (onFinished != null) onFinished();
       yield return null;
    }
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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Which is more efficient GameObject.Find() or public GameObject?? 3 Answers

Attacking an enemy (2D platformer) 0 Answers

I wrote a script for instanciating a prefab after ontriggerexit() function, but it is instanciated to with a variation in position.y from the empty gameobject i placed - where it should be instanciated 0 Answers

[UI] How to force Content Size Fitter Update after instantiating a prefab? 1 Answer

Object doesn't instantiated right spot 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