• 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
1
Question by Recluse · May 23, 2011 at 02:07 PM · lagframerateslowslowdown

What's causing scene to slow down?

I've started on a new project in which the level geometry is instantiated randomly/procedurally.

Objects are created beyond camera view and when they pass behind the player they are destroyed.

On the iPhone the game starts smoothly at around 40fps but there is a gradual slow down. After two minutes the scene is really chugging.

I'm wondering what is causing the deterioration in speed? I've double checked that every clone is getting destroyed once out of sight and the game reaches a maximum of about 60 objects and handles this fine initially.

Could this be something to do with instatiate and destroy on a constant basis? In my previous game, Surveillant I used these functions only occasionally.

Comment
Add comment · Show 4
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 demize2010 · May 23, 2011 at 02:12 PM 1
Share

Certainly sounds like something is getting stuck in memory... have you tried running the level with out the instancing?

avatar image HHameline · May 23, 2011 at 02:36 PM 0
Share

Sounds like a memory leak to me.

avatar image Joshua · May 23, 2011 at 03:48 PM 0
Share

I don't know what's causing this gradual slowdown, so I can't help you there. But for what you are describing I'd simply use procedural mesh generation ins$$anonymous$$d of instantiating objects.

avatar image Recluse · May 23, 2011 at 06:32 PM 0
Share

Don't think using procedural mesh generation for this will work as I need every object to be a separate item with a rigidbody attached.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Recluse · May 23, 2011 at 03:39 PM

I've just built an alternate version, where all the objects are instanced on startup - no destroy or instantiate takes place during play. It runs at a constant rate without any slowdown. This leads me to believe using instantiate/destroy every update is causing the slowdown. Seems a shame, as instantiate/destroy worked fine to generate my random landscape. However I reckon I can write a work-around to achieve the same effect without using the two functions.

Anyone know why instantiate/destroy causes this gradual slow down exactly or how to patch it?

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 pyro · May 23, 2011 at 04:43 PM 0
Share

When you instantiate, you're creating an object from memory. when you destroy it, you arent actually destroying it from memory, you're just flagging it that you dont need it anymore, the garbage collect is what actually frees up the memory. If you are constantly creating and destroying every update then your heap memory is going to go up and up, and the garbage collector is probably not going to run on it's own until you use up your allocated memory, which is going to start causing your game to lag.

avatar image Recluse · May 23, 2011 at 06:34 PM 0
Share

Thanks for that info - very helpful. Should I try garbage collecting manually? I think the way to go is with instantiating everything at the start and then shuffling the deck so to speak.

avatar image pyro · May 23, 2011 at 06:45 PM 0
Share

Yeah you could try calling it manually, but that wont guarantee a collection. Calling the GC manually just acts as a suggestion that it should collect. I agree that the way to go would probably be to instance everything at the start and rearrange it. In my own projects I almost never have to manually call the GC and just let it run automatically when a new level is loaded

avatar image Recluse · May 23, 2011 at 09:18 PM 1
Share

I agree, instantiate/destroy seems to cause a lot of problems speed-wise. I figure I will instance everything on the start and then as objects fall behind the player I will place them upfront again just out of view and change their attributes/positioning etc. That way I get a constant speed rate throughout the game.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Why does simple animation make my game lag? 2 Answers

Sometimes IDE is laggy 0 Answers

Slow for loop 1 Answer

Connecting to internet slows down my game? 1 Answer

GUI.Lable slowdown problem 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges