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

Get Two TMP but in same GameObject

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

  1. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    Hello,

    I would like to get two TextMeshPro gameobjects contained in the same parent gameobject, I searched for solutions I found nothing conclusive.

    If someone wants to help me, I thank.

    here is the code :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class ShowFinalResult : MonoBehaviour
    7. {
    8.     /////////////////// Private Section //////////////////
    9.     private GameObject Manager;
    10.     private GameObject PlayersManager;
    11.     //////////////// Player ///////////////////////////
    12.     [SerializeField]
    13.     private Transform playersContainer;
    14.     public GameObject PlayerPrefabs;
    15.     public GameObject[] PlayerInput;
    16.  
    17.     void Start()
    18.     {
    19.         Manager = GameObject.Find("GameManager").gameObject;
    20.         PlayersManager = GameObject.Find("PlayersManager").gameObject;
    21.  
    22.         for (int i = 0; i < Manager.GetComponent<GameManager>().PlayerNumber; i++){
    23.             GameObject tempListing = Instantiate(PlayerPrefabs, playersContainer);
    24.         }
    25.  
    26.         PlayerInput = GameObject.FindGameObjectsWithTag("PlayerList");
    27.  
    28.         int y = 0;
    29.         foreach (GameObject Player in PlayerInput)
    30.         {
    31.             Player.FindChild("NameText").GetComponent<TMP_Text>().text = "testname";
    32.             Player.FindChild("ResultText").GetComponent<TMP_Text>().text = "testreult";
    33.         }
    34.     }
    35. }
    and the error message : Assets\Scripts\ShowFinalResult.cs(31,20): error CS1061: 'GameObject' does not contain a definition for 'FindChild' and no accessible extension method 'FindChild' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)
     
  2. BenniKo

    BenniKo

    Joined:
    Mar 24, 2015
    Posts:
    100
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Always best to start with the documentation:

    https://docs.unity3d.com/ScriptReference/Transform.GetChild.html

    Note how that is an instance method on Transform, NOT on GameObject where you are trying to use it in lines 31 and 32 above.

    Fortunately every GameObject has a shortcut to Transform instance on it called
    .transform
    !

    This means this line:

    Player.FindChild("NameText").GetComponent<TMP_Text>().text = "testname";


    should instead be:

    Player.transform.FindChild("NameText").GetComponent<TMP_Text>().text = "testname";


    HOWEVER... don't make long hairy lines of code like that because when something goes wrong at runtime, such as a null reference, it's impossible to reason about.

    How to break down hairy lines of code:

    http://plbm.com/?p=248
     
    BenniKo likes this.
  4. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    Thank you for your answers.

    As you recommended Kurt-Dekker, I did go and see the docs, I tried to replace FindChild by Find and I tried to make FindChild precede .transform.

    Unfortunately in these two cases my problem persists.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749