1
\$\begingroup\$

If I create a simple object hierarchy in my scene...

-ExampleGameObject
--Child1 [is active]
--Child2 [is not active]

...how can I accurately store this as a prefab? If I drag ExampleGameObject to a new prefab, then instantiate that prefab, both Child1 and Child2 will be active, which isn't what I want.

\$\endgroup\$
  • 3
    \$\begingroup\$ I'm voting to close this question as off-topic because it's no longer reproducible in recent versions of Unity (4.x & 5.x). Simply changing the enabled state of a child object and updating the prefab correctly sets the state of that child when the prefab is instantiated. I recommend closing this question because without an accepted answer, the community user will keep bumping it to the front page, even though it's no longer relevant. \$\endgroup\$ – DMGregory Feb 18 '17 at 1:58
  • \$\begingroup\$ @DMGregory I'm not sure there is an 'obsolete' close reason; maybe a mod could give the appropriate close reason when closing it, though! \$\endgroup\$ – Vaillancourt Feb 18 '17 at 13:20
  • \$\begingroup\$ @AlexandreVaillancourt I could only find one oblique reference to this type of situation on Meta, from Josh Petrie: "In the cases where the question itself can become outdated to the point of non-usefulness, I still mainly [think] that's a case for closing due to the topic being too localized" \$\endgroup\$ – DMGregory Feb 18 '17 at 16:01
3
\$\begingroup\$

Annoyingly, the simplest way is to attach a script to one of the prefab GameObjects that set the enabled flag manually when the object is dropped into the editor.

using UnityEngine;
using System.Collections;

#pragma strict

public class SetInactive : MonoBehaviour {

    #if UNITY_EDITOR

    void OnDrawGizmos()
    {   
        this.gameObject.SetActiveRecursively(false);
    }

    #endif
}


#endif
\$\endgroup\$
1
\$\begingroup\$

Yes you can! If you uncheck the checkmark next to the name of the game object that you want disabled in the inspector. There is also a way to do it via code without checking, i think you just call the gameObject you want to be disabled and set .active to false

enter image description here

\$\endgroup\$
  • \$\begingroup\$ This doesn't answer my question - try my scenario, create a prefab and drag on the ExampleGameObject. When you create a new instance of the prefab, both objects will be enabled. \$\endgroup\$ – Matt Bond Jul 24 '12 at 18:14
  • \$\begingroup\$ Im not exactly sure if this helps any but you could try and uncheck the mesh renderer for object in the prefab. Its technically still there, just not being displayed though. \$\endgroup\$ – Mungoid Jul 24 '12 at 19:04
  • \$\begingroup\$ This was my 'resort to' option, it's alright in isolation, but clutters my calling code. (I have an "enable object" script, which is used often, a generic "enable x component in object" script would be either time consuming or messy) Good advice for the times it's unavoidable though! \$\endgroup\$ – Matt Bond Jul 25 '12 at 6:39
  • \$\begingroup\$ OnEnable() is not being called after calling .SetActive(true). Any idea? \$\endgroup\$ – Nasir Mahmood Aug 20 '14 at 8:18
  • \$\begingroup\$ In this case there is no way to re-activate the child because you can't "Find" an inactive child or assign a Prefab component to a variable before Instantiate, which means you won't have any reference to activate a disabled Child in a Prefab. \$\endgroup\$ – Guney Ozsan Feb 17 '17 at 13:17
1
\$\begingroup\$

You need to instantiate the Prefab while the Child is active, assign it to a variable and then set the Child inactive.

Otherwise there is no way to activate the Child since you can't assign a Prefab component to a variable before Instantiate to reach it later, and also you can't "Find" an inactive GameObject to make it active. Either way you won't be able to get any reference to activate a disabled Child in a Prefab.

Pseudo Unity 3D C# code:

Define a variable in a script attached to the main component of the prefab:

public class MyPrefab {
    public GameObject problemChild;
}

In your game code, while instantiating that prefab assign the child to the variable in the prefab and then disable the child:

Transform newTransform;

void InstantiateMyPrefab() {
    newTransform = Instantiate(myPrefab, position);
    newTransform.problemChild = newTransform.FindChild("ProblemChild").gameObject;
    newTransform.problemChild.SetActive(false);
}

So it will be instantiated with the child disabled by default and you'll have a reference to that child.

Later in the game you can call this to make the child active again.

void WhereIsMyChild() {
    newTransform.problemChild.SetActive(true);
} 
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.