Search Unity

Cannot change TextMeshPro text

Discussion in 'Multiplayer' started by dkalkwarf, Mar 8, 2023.

  1. dkalkwarf

    dkalkwarf

    Joined:
    Mar 29, 2020
    Posts:
    13
    I have a playing card prefab which contains a TextMeshPro object for displaying a number. I wish to instantiate the prefab and change the number. In some cases I can change the number, in other cases I cannot.

    I started by creating a sample scene to experiment with the code for changing the text. This scene contains an object with a script for changing the text. In the Start method of the script I instantiate the prefab and change the number. This works fine. See the code below.

    Code (CSharp):
    1.         Vector3 position = new Vector3(0.0f, 0.0f, 0.0f);
    2.         card = Instantiate(cardPrefab, position, Quaternion.Euler(0.0f, 180f, 0f));
    3.         GameObject.Find("UpperLeftNumber").GetComponentInChildren<TextMeshPro>().text = "88";
    4.  
    When I move the code from the sample scene to my game scene, it does not work. In the game scene, the code is called in a ClientRPC.

    I have tried many combinations of SetActive(true/false), SetText, ForceMeshUpdate, and SetAllDirty. Nothing works, the number doesn't change. If there is a trick to using one or more of these methods, then please help me. Perhaps I am using them in the wrong order?

    There seems to be a distinct difference between instantiating in Start rather than in a ClientRPC. Can someone explain this difference?
     
    Last edited: Mar 9, 2023
  2. dkalkwarf

    dkalkwarf

    Joined:
    Mar 29, 2020
    Posts:
    13
    Problem solved. There were two objects name UpperLeftNumber in my scene. Switching from
    Code (CSharp):
    1. GameObject.Find(...)
    to
    Code (CSharp):
    1. card.GetComponentsInChildren()...
    solved the problem.