Unity gameobject.activeself, Gameobject.activeinhierarchy,gameobject.setactive and setactiverecursively

Source: Internet
Author: User

Activeself (Read only): The active state of the object itself, which corresponds to whether its checkbox in Inspector is checked
Activeinhierarchy (Read only): Whether the object is active in the hierarchy. That is, to make this value true, the activeself state of this object and all its parent objects (and ancestral objects) is true.

An object is visible (not hidden) in the scene, so that not only its own activeself is true, but the activeself state of all its parent objects (and ancestral objects) is true.

Summarize:
The Activeinhierarchy state represents the actual active state of the object in the scene. It actually represents the activeself state of an object and all its ancestral objects. The activeself corresponds to whether the checkbox in the Inspector is checked

The Activeself state represents the Activeself state of the object itself, so when the object itself is activeself true, and the activeself state of all its ancestors is not all true, The Activeinhierarchy state of this object is false.

activeself== object itself
activeinhierarchy== the object itself and all its ancestral objects = = Whether the object actually activates in the scene

As for SetActive, the activeself state of the object itself is changed, so when an object setactive, it may not be activated in the scene because its ancestor object may be inactive.
setactiverecursively, changing the activeself state of the object itself and all its sub-objects, is equivalent to invoking setactive on the object itself and all its sub-objects.
Since setactiverecursively is obsolete (obsolete), the future will be removed, so when you set the active state of an object and all its sub-objects, you can call the method

void Deactivatechildren (Gameobject g, bool a) {    g.activeself = A;        foreach (Transform child in G.transform) {        Deactivatechildren (Child.gameobject, a);}    }
Advanced Skill:

Using Extension Method

public static class extensions{public    static void Setactivateforallchildren (this gameobject go, bool state)    { C2/>deactivatechildren (go, State);    }    public static void Deactivatechildren (Gameobject go, bool state)    {        go. SetActive (state);        foreach (Transform go.transform)        {            Deactivatechildren (child.gameobject, State);}        }}    

  

Now, Can use the like:

Public  class Mytest:monobehaviour {public Gameobject go;//with this for    initializationvoid Start () {        // Obsolete        //go. Setactiverecursively (true);        Go. Setactivateforallchildren (True);}}

Reference:

http://blog.csdn.net/czlilove/article/details/23827267

Unity 3.5 to 4.0 upgrade Guide Upgrade guides from Unity 3.5 to 4.0

Http://game.ceeger.com/Manual/UpgradeGuide3540.html

Unity gameobject.activeself, Gameobject.activeinhierarchy,gameobject.setactive and setactiverecursively

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.