Reference gameObject from component

My Code:

    GameObject ComposterUI;

    private void Start()
    {
        ComposterUI = FindObjectOfType<CompostUISetting>().gameObject;
    }

The gameObject I’m trying to reference is a UI Canvas. That’s attached to a gameobject as a child of a child.
I thought finding the script and accessing the gameObject would be the fastest, but I’m stuck. I’m trying to access it from an instantiated prefab.

I’ve found an other forum post where they referenced the gameObject from a component. So I’m not sure why this is not working.

I’ve been reading the Unity manual on UI and there was a line: It is a GameObject-based UI system that uses Components and…
If it’s a gameObject based system than it should have GameObjects right?

What you have is correct… IF! It actually finds the gameobject with that component on it.

Remember that inactive gameobjects aren’t found by default, so you need to use the proper call if the object with CompostUISetting is on it is turned off.

1 Like

Thanks
I have to enable it first to access it. I guess I need a middle man to find first like the parent of it and enable it from there.