无法更改Unity TextMeshPro文本。

huangapple go评论64阅读模式
英文:

Cannot change Unity TextMeshPro text

问题

我有一个扑克牌预制件,其中包含一个 TextMeshPro 对象用于显示数字。我希望实例化这个预制件并更改数字。在某些情况下,我可以更改数字,在其他情况下则不行。

我首先创建了一个示例场景,用于尝试更改文本的代码。该场景包含一个带有更改文本脚本的对象。在脚本的 Start 方法中,我实例化了预制件并更改了数字。这运行良好。见下面的代码。

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";

当我将代码从示例场景移动到我的游戏场景时,它不起作用。在游戏场景中,该代码在 ClientRPC 中调用。

我尝试过许多组合,包括 SetActive(true/false),SetText,ForceMeshUpdate 和 SetAllDirty。没有什么起作用,数字不会改变。如果使用这些方法中的一个或多个有诀窍,请帮助我。也许我是按照错误的顺序使用它们?

在 Start 中实例化与在 ClientRPC 中实例化之间似乎存在明显的差异。有人能解释一下这种差异吗?

英文:

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(&quot;UpperLeftNumber&quot;).GetComponentInChildren&lt;TextMeshPro&gt;().text = &quot;88&quot;;

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?

答案1

得分: 1

如果你有ClientRPC,只能从服务器机器调用。看起来你正在制作一个多人应用程序/游戏。

你可以在这里找到更多信息:https://docs-multiplayer.unity3d.com/netcode/current/advanced-topics/message-system/clientrpc/index.html

如果这不是问题,你可以使用刚刚生成的对象来检索TextMeshPro。这是在你的场景中有多个对象被命名为“UpperLeftNumber”的情况。

Vector3 position = new Vector3(0.0f, 0.0f, 0.0f);
card = Instantiate(cardPrefab, position, Quaternion.Euler(0.0f, 180f, 0f));

Transform[] transforms = card.GetComponentsInChildren<Transform>();

foreach(Transform t in transforms){
  if(t.name == "UpperLeftNumber"){
   t.GetComponentInChildren<TextMeshPro>().text = "88";
}
}
英文:

If you have an ClientRPC this can be called only from a server machine. Seems like you are making a Multiplayer application/game.

You can find more info here https://docs-multiplayer.unity3d.com/netcode/current/advanced-topics/message-system/clientrpc/index.html

If that's not the issue you can use the object that you just spawned to retrieve the TextMeshPro. This in case you have multiple objects in your scene named as "UpperLeftNumber".

 Vector3 position = new Vector3(0.0f, 0.0f, 0.0f);
 card = Instantiate(cardPrefab, position, Quaternion.Euler(0.0f, 180f, 0f));

Transform[] transforms = card.GetComponentsInChildren&lt;Transform&gt;()

foreach(Transform t in transform){
  if(t.name.equals(&quot;UpperLeftNumber&quot;)){
   t.GetComponentInChildren&lt;TextMeshPro&gt;().text = &quot;88&quot;;
}

}

答案2

得分: 0

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()...

英文:

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()...

huangapple
  • 本文由 发表于 2023年3月9日 22:53:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定