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.
Vector3 position = new Vector3(0.0f, 0.0f, 0.0f);
card = Instantiate(cardPrefab, position, Quaternion.Euler(0.0f, 180f, 0f));
GameObject.Find("UpperLeftNumber").GetComponentInChildren<TextMeshPro>().text = "88";
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?
Both responders hit on the problem: there are two UpperLeftNumber objects in the scene.
The fix was just what Giuseppe provided: use card.GetComponentsInChildren()
rather than GameObject.Find()...