Search Unity

issues with SetActiveRecursively

Discussion in 'Editor & General Support' started by cchaos_jond, Feb 15, 2012.

  1. cchaos_jond

    cchaos_jond

    Joined:
    Feb 15, 2012
    Posts:
    4
    I was having problems with child objects not disabling properly from SetActiveRecursively.

    Specifically, I was getting this error every frame when one of those children had an emitting ParticleSystem:

    "UpdateParticle system should not happen on disabled GO"

    It also just seemed like OnDisable wasn't being called for the children.

    For now, I'm getting around it by adding my own recursive activate/deactivate extension functions.

    Plugins\GameObjectExtensions.cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public static class GameObjectExtensions
    6. {
    7.     public static void ActivateRecursively(this GameObject go)
    8.     {
    9.         go.ActivateRecursively(false);
    10.     }
    11.    
    12.     public static void ActivateRecursively(this GameObject go, bool playParticleSystems)
    13.     {
    14.         int childCount = go.transform.childCount;
    15.         Transform child = null;
    16.         for ( int i = 0; i < childCount; i++ )
    17.         {
    18.             childCount = go.transform.childCount;
    19.             if ( i >= childCount )
    20.                 i--;
    21.            
    22.             child = go.transform.GetChild(i);
    23.             if ( null != child )
    24.             {
    25.                 child.gameObject.ActivateRecursively(playParticleSystems);
    26.             }
    27.         }
    28.        
    29.         if ( playParticleSystems )
    30.         {
    31.             ParticleSystem[] particleSystems = go.GetComponents<ParticleSystem>();
    32.             foreach ( ParticleSystem particleSystem in particleSystems )
    33.             {
    34.                 particleSystem.Play();
    35.             }
    36.         }
    37.        
    38.         go.active = true;
    39.     }
    40.    
    41.     public static void DeactivateRecursively(this GameObject go)
    42.     {
    43.         go.DeactivateRecursively(false);
    44.     }
    45.    
    46.     public static void DeactivateRecursively(this GameObject go, bool stopParticleSystems)
    47.     {
    48.         int childCount = go.transform.childCount;
    49.         Transform child = null;
    50.         for ( int i = 0; i < childCount; i++ )
    51.         {
    52.             childCount = go.transform.childCount;
    53.             if ( i >= childCount )
    54.                 i--;
    55.            
    56.             child = go.transform.GetChild(i);
    57.             if ( null != child )
    58.             {
    59.                 child.gameObject.DeactivateRecursively(stopParticleSystems);
    60.             }
    61.         }
    62.        
    63.         if ( stopParticleSystems )
    64.         {
    65.             ParticleSystem[] particleSystems = go.GetComponents<ParticleSystem>();
    66.             foreach ( ParticleSystem particleSystem in particleSystems )
    67.             {
    68.                 particleSystem.Stop();
    69.                 particleSystem.Clear();
    70.             }
    71.         }
    72.        
    73.         go.active = false;
    74.     }
    75. }
    76.  
    Just wondering if someone has other suggestions.
     
    Last edited: Mar 12, 2012
  2. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    How would I use this work around script?

    *** Update No problem got it!

    Wow learnt something new C# lets you extend classes without using inheritance, cool!

    Still does not resolve my Update Particle System error problem thought!
     
    Last edited: Mar 10, 2012
  3. cchaos_jond

    cchaos_jond

    Joined:
    Feb 15, 2012
    Posts:
    4
    What was your issue with particle systems?

    I had issues with them as well, so I added options to play/stop any particlesystems found.

    I also had issues with children detaching from their parent OnDisable, so there's something added for that (the i-- part)