GameObject.SetActive doesn't work in game build

I’m not 100% sure why this isn’t working but I’ve searched online for this issue. I’ve found a few possible solutions but I wanted to ask here first.
I’ve made an avatar creator in Unity3D and it all works brilliantly in the Editor, except for this annoying error: GetComponent requires that the requested component ‘GameObject’ derives from MonoBehaviour or Component or is an interface. I found out where this is going wrong. My clothing objects appear with an assigned button click that’s triggered using SetActive. Also, I used GetComponentInChildren to locate the object in question because it’s a child of an object (specifically my avatar’s body; the clothing objects are separated meshes).
I think I must have done this GetComponent wrong because in my build none of the objects will show up when I click the buttons, but they worked fine in the editor. Is there something I’ve forgotten?
Here’s an example of a script that has a GetComponent method. Several scripts are similar to this depending on the object needed to show.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Hairstyle1Button : MonoBehaviour {
   
    public GameObject hairstyle1;
   
    // Use this for initialization
    void Start () {
        hairstyle1 = GetComponentInChildren<GameObject> ();
        hairstyle1.SetActive (false);
       
    }
   
    public void ShowObject(){

        hairstyle1.SetActive (!hairstyle1.activeInHierarchy);
    }
   
    // Update is called once per frame
    void Update () {
       
    }
}

Any help would be greatly appreciated. Thank you.

GameObject is not a component, so you can’t do GetComponent().

You’ll need to either find a specific component of the GameObject, find the GameObject by name, drag the reference directly to the variable in the inspector, or get a child by index. There’s tons of ways to reference other objects.

For a simple change you can probably do “transform.GetChild(0).gameObject”

That looks awkward to me because a GameObject is technically not a Component, and so I would expect GetComponent() to fail. The documentation implies this, but it’s not super clear about it. And the error message you provided suggests otherwise, but the message itself might be inaccurate.

If you simply want the first child, I’d recommend using transform.GetChild(0).gameObject.

Okay, thanks for all your suggestions so far. So I guess that you recommend changing line 11 to transform.GetChild(0).gameObject? I’ll look into it.
I found the original code from another forum. I used it because it seemed to work and I didn’t know what the error meant, so thanks for letting me know that it wasn’t a good statement to use. I’ll see if I can change it (successfully).

I promote understanding, not just solutions. I want you to enjoy your time with Unity and it’ll be more fun if you take the time to understand it. I really recommend not trying to make a game and just learning about the engine at first.

A GameObject has no hierarchy, it is only a reference to a group of components. The Transform component is what knows who the children are. The error is suggestive of this, but may be difficult to understand if you haven’t studied types a bit yet.

(put this on a gameobject with some child empty gameobjects)

public class QuickTest : MonoBehaviour {

    GameObject foo;
    void Start ()
    {
        foo = GetComponentInChildren<Transform>().gameObject;
        if (foo = this.gameObject) { Debug.Log("It's this one, doh"); }
    }
}

This works because all Components have a reference to their parent GameObject. You can see that if you ctrl-f gameObject on the Transform’s page… Unity - Scripting API: Transform

However, if we look at the API… Unity - Scripting API: Component.GetComponentInChildren unfortunately it includes the parent. This is the sort of thing they would probably change except it would break a bunch of stuff and confuse established users hindsight is 20/20. At least I hope so!

There are a bunch of ways around this. We could go someTransform = this.transform.Find(“name of child”), and then search using that transform. In fact, in general it might make more sense to have a Transform reference saved in your script to use for searching for stuff!

public class QuickTest : MonoBehaviour {

    GameObject foo;
    void Start ()
    {
        foo = GetComponentInChildren<Transform>().gameObject;
        if (foo = this.gameObject) { Debug.Log("It's this one, doh"); }
    }

    GameObject bar;
    private void Awake()
    {
        bar = this.transform.GetChild(0).gameObject;
        if (bar != this.gameObject) { Debug.Log("It's somebody else!"); }
    }

}

Let’s try to understand. Bar is a GameObject, so just like every equation, the other side needs to also be a GameObject. We start from this, which gives you a GameObject (bar = this; is OK)

but then we look at its Transform, because we’d like to use Transform.GetChild(), which gives us the first child.

But GetChild gives a Transform, and we want a GameObject, so we use that Transform’s reference to a GameObject, .gameObject.

Don’t worry too much about it now, but notice if you click “class in UnityEngine / Inherits from:” under the API page titles it takes you up to Object. The way Object-Oriented Programming works is essentially you have a basic thing and then you make more specific things, each of which includes the stuff their parent does. Inheritance is closely related to polymorphism, which is the idea that a parent can have some method and then children can override it and do different things. Those are the two important concepts you should think about as a new lone programmer, since the other “pillar” of OOP, encapsulation, is mostly concerned with keeping stuff from breaking because someone/something does unintended stuff to it.

Thanks, JayMounes. I’m quite new to C# programming and I haven’t learnt everything yet. I’m getting better but I’m not there yet. Child objects isn’t something I’ve covered yet so this has been really useful. I’ve been studying C# for the last year or two and I have a better understanding of it than I did before but this is an area I haven’t touched on yet. I’m really pleased how quickly I got responses. I’ll take all your suggestions on board and work on my code, and do some more studying. I’m about halfway through this really good book that covers C# in Unity and it’s been a big help. Thanks again, I really appreciate it.

I was having the same problem with cloth, and it’s not about your code!

Click on your afected mesh your Project tab (not in the hirarchy) → Click on the Model tab → Active Read/Write enabled

Found the solution here: All cloth objects missing in build - Questions & Answers - Unity Discussions