Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Reference gameObject from component

Discussion in 'Scripting' started by Daviiid, Jan 3, 2023.

  1. Daviiid

    Daviiid

    Joined:
    Oct 9, 2015
    Posts:
    50
    My Code:
    Code (CSharp):
    1.     GameObject ComposterUI;
    2.  
    3.     private void Start()
    4.     {
    5.         ComposterUI = FindObjectOfType<CompostUISetting>().gameObject;
    6.     }
    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?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Daviiid likes this.
  3. Daviiid

    Daviiid

    Joined:
    Oct 9, 2015
    Posts:
    50
    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.