playMaker

Author Topic: Activate/Deactivate Vs Spawn/Destroy performance for Mobile issues  (Read 3893 times)

Spunkyinteractive

  • Playmaker Newbie
  • *
  • Posts: 22
TL;DR Activating objects causes lag spikes, but having everything active at once causes a big hit to CPU. Good method for maintaining performance when dealing with many game objects?

I'm in the middle of optimizing my game to get it up to a rock solid 30fps. In my quest for achieving this i've deactivated all objects in my scene that are not being used or are not currently visible to the camera. Deactivating everything instantly gave me a big boost in fps however what i am seeing is that when i activate one of the parent objects i get a big lag spike. I see from the profiler that activating an object seems to cause quite a bit of garbage collection.

Does anyone have any experience with activating/deactivating objects vs just spawning in prefabs and destroying them when finished?

What is the best way to go about keeping performance up?

Having everything activated all the time keeps the fps smooth but even though they are not being rendered on camera it causes a huge hit to cpu, and activating on the fly seems to cause lag spikes. Is there no method that gives the happy medium of low cpu usage and no lag spikes?

-Stephen

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Activate/Deactivate Vs Spawn/Destroy performance for Mobile issues
« Reply #1 on: February 05, 2017, 09:50:04 AM »
Hi

Did you ever find out why? i have the same issue and i can only replicate this using OnTrigger events, well i could be wrong but if i add a trigger enter to a fsm and activate/deactivate the game object it causes a spike.

Cant figure out why at the moment. grr

Nick

elusiven

  • Full Member
  • ***
  • Posts: 233
  • Debt we all must pay...
Re: Activate/Deactivate Vs Spawn/Destroy performance for Mobile issues
« Reply #2 on: February 05, 2017, 10:06:21 AM »
Use object pooling.

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Activate/Deactivate Vs Spawn/Destroy performance for Mobile issues
« Reply #3 on: February 05, 2017, 05:56:02 PM »
thanks, not read about it yet, do you use an asset or your own scripting?

I have a scene where i have placed all my prefabs in place where i want them to be and cant spawn them during runtime, just need to disable them when they are so far from the camera. Do you think object pooling will work for me?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Activate/Deactivate Vs Spawn/Destroy performance for Mobile issues
« Reply #4 on: February 06, 2017, 01:02:37 AM »
Hi,

 It really depends what going on when activating, activating in itself isn't the problem, it's what all the behaviors are going to do when they get activated. so this is where you need to investigate.

-- look at the GameObject you activate, and see what's done at the beginning. Typically, the usual issue is setup, where you try to find objects in the scene by name, and that's known to be impacting perfs.

Bye,

 Jean

elusiven

  • Full Member
  • ***
  • Posts: 233
  • Debt we all must pay...
Re: Activate/Deactivate Vs Spawn/Destroy performance for Mobile issues
« Reply #5 on: February 06, 2017, 05:05:00 AM »
I use a modified version of this

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

Although you can buy ready pooling systems, there is even a free one here:

https://www.assetstore.unity3d.com/en/#!/content/28002

But as Jean mentioned, Activating is not as bad as instantiating/destroying, so really you would only use object pooling if you are instantiating and destroying a lot of objects.

This is so right though, I always have many "Find GameObject" in the first state of my FSMs, and for me that's the problem when it spikes up. Jean, is there actually a way to reduce this? The only thing that I can think of is the find with tag, but that means that I would need +100 tags :P

marv

  • Junior Playmaker
  • **
  • Posts: 50
Re: Activate/Deactivate Vs Spawn/Destroy performance for Mobile issues
« Reply #6 on: February 06, 2017, 12:01:49 PM »
Couldn't you just dynamically set up a bunch of Arrays/Hash tables containing your objects on game start, depending on your needs and then use those to access all objects within the specific array you need at any given time?