Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

How do I access the TEXT component on a child GameObject?

Discussion in 'Scripting' started by QuickiSticki, Mar 9, 2021.

  1. QuickiSticki

    QuickiSticki

    Joined:
    Nov 18, 2020
    Posts:
    16
    Hello all! I am trying to work on a tooltip system, but I am having trouble getting the components from my child GameObject. I am trying to access the TEXT component using code that another person gave me, but it is not working. I know I am missing some piece of the code that he forget to tell me, and was hoping you guys could help!

    Here is the code:

    toolTipPrefab.itemName.text = _slot.ItemObject.data.Name;

    The error I am getting is:

    Assets\MyScripts\Scriptable Objects\Scripts\UserInterface.cs(147,27): error CS1061: 'GameObject' does not contain a definition for 'itemName' and no accessible extension method 'itemName' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)

    Is it easier to create a definition for itemName, or can I just access it straight in the line of code?

    Thank you!
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    6,227
    Well, from the error. toolTipPrefab is a GameObject. My guess is toolTipPrefab was suppose to be a component reference which contains a public variable of Text named itemName. But that's my guess.

    Either way, you need to use GetComponent and get the proper component reference or change toolTipPrefab to be of the type of that component and drag and drop.
     
unityunity