Get Two TMP but in same GameObject

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 :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class ShowFinalResult : MonoBehaviour
{
    /////////////////// Private Section //////////////////
    private GameObject Manager;
    private GameObject PlayersManager;
    //////////////// Player ///////////////////////////
    [SerializeField]
    private Transform playersContainer;
    public GameObject PlayerPrefabs;
    public GameObject[] PlayerInput;

    void Start()
    {
        Manager = GameObject.Find("GameManager").gameObject;
        PlayersManager = GameObject.Find("PlayersManager").gameObject;

        for (int i = 0; i < Manager.GetComponent<GameManager>().PlayerNumber; i++){
            GameObject tempListing = Instantiate(PlayerPrefabs, playersContainer);
        }

        PlayerInput = GameObject.FindGameObjectsWithTag("PlayerList");

        int y = 0;
        foreach (GameObject Player in PlayerInput)
        {
            Player.FindChild("NameText").GetComponent<TMP_Text>().text = "testname";
            Player.FindChild("ResultText").GetComponent<TMP_Text>().text = "testreult";
        }
    }
}

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?)

I do not think your problem is with TextMeshPro.
The error says that there is not method called “FindChild”.
You probably wanted to use “Find”

Always best to start with the documentation:

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

1 Like

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.

As always, how to understand compiler and other errors and even fix them yourself:

https://forum.unity.com/threads/ass…3-syntax-error-expected.1039702/#post-6730855