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 PixelSpartan · Feb 12, 2017 at 07:38 PM · androidinstantiatemobileloadinginstantiating

Instantiating thousands on start (loading custom levels)

Is there any alternative to having to instantiate every object when "loading" a user made level? Having to instantiate a lot, especially on mobile causes the game to get slower and slower. So is there any way to do this? how does unity load its scene without slowing the game down (after it loaded)

Comment
Add comment · Show 3
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 RobAnthem · Feb 12, 2017 at 07:45 PM 0
Share

Are you loading the objects on courotines? If so, and still feeling the lag, have you considered a loading bar?

avatar image PixelSpartan RobAnthem · Feb 13, 2017 at 07:21 PM 0
Share

Doesnt matter if coroutines , if you instantiate a lot, every time you instantiate the game becomes slower , as the Instantiating takes up more and more memory, so loading bars wont help

avatar image RobAnthem PixelSpartan · Feb 13, 2017 at 07:52 PM 0
Share

What I meant is, for example. To create a coroutine that instantes a number of objects at a time, and waits for a moment, then does it again, until all objects are in the scene. If your problem is that having all your objects in the scene is too laggy, then that means your objects are consu$$anonymous$$g too much data for the actions you are attempting.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by AMU4u · Feb 12, 2017 at 07:45 PM

Other than your own customized optimizations, your best bet really is Object Pooling.

https://unity3d.com/learn/tutorials/topics/scripting/object-pooling

The basic idea being that you create these hundreds of objects on awake, and reuse and recycle them depending on your needs.

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 PixelSpartan · Feb 13, 2017 at 07:24 PM 0
Share

Object pooling every single possible prefab a user may put in the scene is worse than instantiating like 100 objects at start, because we are talking about a level editor not commonly used projectiles. Having like 10 available objects for like 100 prefabs would mean having 1000 objects in the scene rather than instantiating just 100

avatar image
0

Answer by Hanoble · Feb 13, 2017 at 08:02 PM

Hello PixelSpartan, I like the name. Now to business, you say:

"if you instantiate a lot, every time you instantiate the game becomes slower , as the Instantiating takes up more and more memory".

However, that should not really be the case, and just to be sure let's make sure we are on the same page.

For starters, are you experiencing poor frame rate or the fact that loading is simply taking too long? Instantiating objects may cause an obvious hiccup in gameplay, especially on mobile, but once the object is instantiated it should not be the case that the game remains running slow due to memory. However, could it be that you are having FPS issues (for example bringing in 1000 objects and then trying to draw the vast majority of them without proper batching)?

If you are having issues with long loading times, as stated by RobAnthem you, could use a coroutine to help load additional objects after the scene transition. For example, loading the main pieces of a scene behind a loading screen for a few seconds, and then getting the player in the game while continuing to load objects via coroutines after they are in and playing. With this approach your player might see some popping in of the new objects, and depending on how many objects it may still slow your game too much, but it is a solution that works for some people.

On the other hand, if the game remains slow, I suspect you may have an FPS issue. Take a look at your set pass calls and if you see them up over a couple hundred and are targeting low end mobile, this may be your issue. The first step here would be to look into static/dynamic batching. Another possibility is that you are using shaders that are too performance intensive on mobile, which could also severely degrade your FPS. Lastly, depending on what these objects are doing, it could be possible that you are generating a lot of garbage via scripts. The constant garbage collection could also degrade performance, and you can take a look at profiling on device to see which areas to best optimize.

In general, having a lot of objects in memory SHOULD NOT cause your game to be slow. I have been a part of mobile games with several thousands of objects in them and pushing the memory limits, while still maintaining a solid FPS on older mobile devices (iPad2, iPhone4, Samsung Galaxy Tablets, etc) by following those rules outlined above.

Comment
Add comment · Show 2 · 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 PixelSpartan · Feb 13, 2017 at 10:30 PM 0
Share

Well i guess using up memory when creating many objects is expected and cant be avoided

avatar image Hanoble PixelSpartan · Feb 13, 2017 at 11:56 PM 0
Share

This is correct, but typically the objects themselves are very small and having thousands of them, even on mobile, is not a concern. However, depending on the assets these objects use (textures, audio clips, animation, etc), that is typically where the memory begins to bloat out and cause out of memory and crashing issues.

avatar image
0

Answer by PixelSpartan · Feb 13, 2017 at 10:08 PM

Im talking about instantiating a lot of objects and then the framerate remaining low. And the more levels you load over time the game goes slower and slower

And i try as much to batch everything so its not that and i dont really care about load time

My guess is that the more objects you make the more memory it takes up, so it lags as it struggles to find memory to run well

Comment
Add comment · Show 4 · 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 PixelSpartan · Feb 13, 2017 at 10:12 PM 0
Share

Probably bad GC, maybe i can try GC.Collect(); and see if it helps

avatar image RobAnthem PixelSpartan · Feb 13, 2017 at 11:00 PM 0
Share

GC.Collect() is highly advised against unless you have very good reasons, I don't see much that would end up in the GC from the instantiations, because the objects should all be loaded into the stack, and have no reason to be in the heap unless destroyed.

avatar image Hanoble PixelSpartan · Feb 13, 2017 at 11:31 PM 0
Share

GC issues would come down to your own custom code, not those instantiations. If the poor framerate was due to constant GC collections, you could verify this by profiling, or even disabling all the scripts you can and see if it helps.

avatar image Hanoble · Feb 13, 2017 at 11:13 PM 0
Share

I would really take a look at the set pass calls if experiencing constant low FPS, as it is typically the #1 mobile issue. I would highly advise to profile on device, as right away you will be able to see what is wrong.

You are correct in that the more objects you create, especially if they are complex objects, it will require more memory. However, this will not typically cause your game to have poor framerate, ins$$anonymous$$d what will happen (in the case of iOS) is that you will get a "$$anonymous$$emory Pressure" warning from the OS and then eventually your game will crash as it runs out of memory.

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

142 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

Related Questions

Buttons take time to be fully charged at startup 0 Answers

Level load Time on Unity Android (Pro) 1 Answer

How to reduce scene load time on mobile? 2 Answers

What's wrong? Heavy game for Mobile ? 1 Answer

Is changing 60 FPS into 30 FPS affects time in coroutines? 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