SetActiveRecursively() is causing a lag spike, any ideas?

I am using SetActiveRecursively() in my guy for my weapon to turn on a bullet from a pool manager. But when I activate it I am getting a huge lag spike. Am I doing something wrong here or does this normally cause a huge lag spike? Here is a screen shot.

Edit: Idk know the other two images got put in there lol.

1016081--37650--$toasty.JPG
1016081--37651--$Orb.png

I just asked about the spike it causes but was told it is not too bad. Do you have a lot of objects in that parent? Perhaps a for () or Find is better, as setActiveRecursively will activate or deactivate everything in that parent.

It is generally recommended on mobile to cache the objects being activated and activating them individually as renman said.

Also, just something to be aware of, SetActiveRecursively is being deprecated in unity 4. From the unity 4 release notes:

Is it really the same thing under the hood, or do you think there are some actual optimizations regarding GameObject.SetActive() in Unity 4?

No there is nothing parented to it. I changed to to just use active, but I still get a huge lag spike whenever I fire the gun. I might as well use Instantiate. Right Now I create all the objects and turn them off, then once I press the fire button I grab one from the array of bullets and activate it and then it fires for a second then deactivates itself. Once deactivated it can be pulled from the list again. Here is how it have it coded…

Player.cs

GameObject[] projectiles = null;
public int numberOfProjectiles = 5;

void Start()
{
projectiles = new GameObject[numberOfProjectiles];
}

public void ActivateProjectile()
    {
        for (int i = 0; i < numberOfProjectiles; i++)
        {
            if (projectiles[i].active == false)
            {
                projectiles[i].GetComponent<Bullet>().Activate();
                return;
            }
        }
    }

Bullet.cs

public void Activate()
    {
        this.gameObject.active = true;
        this.rigidbody.velocity = Vector3.zero;
        this.rigidbody.angularVelocity = Vector3.zero;
        timeToDie = Time.time + life;
        this.transform.rotation = GameObject.Find("Player").transform.rotation;
    }

Edit: The code never gets formatted right in here lol

.GetComponent and .Find calls are bad for such stuff. You should cache the results once and then reuse em. Manual has section for iOS optimization.

Even when I cache them I still get a lag spike. The profiler is telling me that when I activate the bullet its giving me the spike. I know there is going to be a spike when I activate it, but it shouldn’t be bigger than everything else on the entire screen. This is really getting to me.

Sorry to revive this, but I’m having the same type of issue. It doesn’t matter if I use SetActiveReclusively or .active on the gameobject - there is a lag spike after the object is activated.

I’ve noticed a strange behaviour as well. Here is how it normally goes:

  1. The object gets initiated (Awake and Start get called, since during initialization a custom 2D mesh is being created and UV mapped)
  2. At the end of the Start function, the object deactivates itself.
  3. Later during gameplay another function activates this object (lag spike occurs)

Exception:

  1. The object gets initiated (Awake and Start get called, since during initialization a custom 2D mesh is being created and UV mapped)
  2. At the end of the Start function, the object deactivates itself.
  3. In the editor I enable and disable the object manually (via inspector)
  4. Later during gameplay another function activates this object (lag spike DOES NOT OCCUR)

If I didn’t know better, I’d say that the object simply didn’t initialize itself properly and deactivated prematurely, catching up the moment it is brought back again. That can’t be the case since activating it from the editor doesn’t cause a spike.

Any help?

Im actually interested in this post as well, I have a similar situation in my game right now. Its not really a drastic lag spike but, its still there.

Are you guys using OnEnable at all? (Actually that would solve the idea of getting the component to call a function inside it) About execution order from what I remember first all Awakes are called, and then start + onenable → start + onenable (from object to object both are called before going to the next object). Is that correct (from someone more experienced than me)? I also remember reading that Awake is just called on the first scene compilation, and later on if you pause/unpause only Start runs again… could the same rule apply when an object is re-enabled (running Start all over again)?

Sorry I cant be of a great help to you.
Best regards,
Allan

@Zethariel11 I have been looking into this and I think what is happening is that object isn’t loading itself into memory. I had the same issue with my explosions. I had them turning themselves off at the start and then turning on when I needed them to, but for the first one I still had a noticeable lag spike. The way I fixed the issue and I admit it’s not the cleanest way was to just put all the explosions in the first scene play them and destroy them after 2 seconds. This caused a hiccup at the start but it wouldn’t be noticed since nothing is happening anyways. I had to of course hide them in the background and make sure no sound was played but it made the lag spike from calling SetActiveReclusively go away. Not saying it is going to work but maybe that will help you get rid of it.

@AllanMSmith - I use OnEnable to turn all my stuff on since I am using object pooling and nothing really get’s destroyed. Your right about Awake-Start-OnEnable execution order, but when you re-enable the script start won’t get called again since start get’s called at the start of the creation of the object. So if you use Instantiate start will work, but if you use SetActiveReclusively or active Start won’t work. Hope that helps.