Search Unity

[C#] [Unity 5] How do you enable/disable a child object?

Discussion in 'Getting Started' started by krousty_bat, Feb 3, 2016.

  1. krousty_bat

    krousty_bat

    Joined:
    Oct 9, 2012
    Posts:
    60
    Hello.

    I would like to enable a particle child objects when a collision is checked, and disable it when it's not anymore,

    I have a nul object called PlayerFX_trigger, with a Tag PlayerFX.
    This object have a Box Collider 2d with "Is trigger" activated.
    a Particle System named GroundSmoke is child of this object, with no tag (for now).

    The player is called "Hero", and is the one holding this script:

    Code (CSharp):
    1.     void OnTriggerEnter2D(Collider2D TestContact)
    2.     {
    3.         if (TestContact.CompareTag("PlayerFX"))
    4.         {  
    5.             TestContact.gameObject.SetActive(false);
    6.         }
    7.     }
    When the Player Hero get in contact with the Tag PlayerFX, it is working and deactivate PlayerFX_trigger. But now that it's not active, impossible to continue the collide test anymore, silly me :).
    What I should do is using the SetActive to the child GroundSmoke, but after an afternoon of search and test, impossible for me to find a way to do such an easy task.
    This could stop here, but I was able to find something finally :)

    I declare a public GameObject **Groundsmoke**; that will allow me to place manually the Object that I want to activate or deactivate. and then this is the rest that is working.

    Code (CSharp):
    1.     public GameObject Groundsmoke;
    2.     void Start()
    3.     {
    4.         Groundsmoke.gameObject.SetActive (false);
    5.     }
    6.  
    7.     void Update()
    8.     void OnTriggerEnter2D(Collider2D TestContact)
    9.     {
    10.         if (TestContact.CompareTag ("PlayerFX"))
    11.         {
    12.             Groundsmoke.gameObject.SetActive (true);
    13.         }
    14.     }
    The thing I don't get now, is how to make it disappear when you left the area, for when I try this
    Code (CSharp):
    1.     void OnTriggerExit2d(Collider2D TestExit)
    2.     {
    3.         Groundsmoke.gameObject.SetActive (false);
    4.     }
    The Object Groundsmoke stay enable and visible!
    Is it because I should call a function from FixedUpdate instead?

    I would like to find later too, how to get this Groundsmoke child gameObject, not manually but with a line of script, to be able to actually turn off some parameters over time (stop looping a particle, or changing transparency for instance) to be more subtle, instead of an ON/OFF trigger.

    Thx a lot.
     
  2. NicBischoff

    NicBischoff

    Joined:
    Mar 19, 2014
    Posts:
    204
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You can use Transform.Find("Groundsmoke") for this.
     
  4. krousty_bat

    krousty_bat

    Joined:
    Oct 9, 2012
    Posts:
    60
    Thx guys!

    @JoeStrout:
    I will try, even if it seems It might be better to go with tags instead of "names" for performance reasons, right ?
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    No, not really. String comparisons in C# are blazing fast, unless you have some pathological case, like a lot of long strings of identical length that differ only in the last character.

    And tags are strings too, by the way. :)

    The real problem, no matter how you search for the object, is the searching. Where performance is an issue, you should only do that search once, in the Start method, and then cache the result in a private property. Then the rest of your game, you will be accessing the object directly through that property, with no searching at all.
     
  6. krousty_bat

    krousty_bat

    Joined:
    Oct 9, 2012
    Posts:
    60
    Ok, Thx \o/
    The less I search, the better !
    Unity tutorials are teaching you to go in that direction: caching into private property (or to use tags).
    So, the "manual" way: using a publicGameObject that you will place yourself, is actually not a bad way either, for you don't loose time to look for it, right ?
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Right, that's perfectly fine too. Do whichever is easier or makes the most sense to you.