- Home /
3D TextMesh not updating on .text change
Hi,
I've read a few answers that seem related to this, but the answer I see given a lot is not working for me.
I have a script that declares a var of type TextMesh (I have also done this with a var od type GameComponent, and then used GetComponent to get a reference to the TextMesh, but the problem remains)
When I update the TextMesh by setting the .text field to a new value, it does not update at runtime. However, once I stop the scene from running the text is updated within the editor!
I have seen references to issuing .Commit(), but I believe that is for 2d work? - TextMesh does not have a .Commit() method.
The only answers I can find on the net just say to update TextMesh.text . For me, that doesn't work - the mesh is not updated in the game, but it is in the editor. Windows 7 (I've seen one reference to a bug in Android that has a similar behaviour)
Does anyone know whats going on?
Yes -- sounds like a code thing. Like transform.Text$$anonymous$$esh
isn't a shortcut (have to use transform.GetComponenent()
. And in javascript it won't even give an error -- just won't do anything.
Commit is a whole unrelated thing. I've used lots of Text$$anonymous$$eshes, on iPads, and had all sorts of funny things (like s's tucking close up under the T's in some fonts,) but never had them not change.
The script in question;
//Generate Score
var tm:Text$$anonymous$$esh=prefabScoreText.GetComponent(Text$$anonymous$$esh);
var score:int=parseInt(tm.text);
tm.text=(score+10).ToString();
Where prefabScoreText is a 3D Text object prefab dragged into the script slot (the 'live' instance of the 3D text prefab is dragged from the same prefab and made a child of the camera in the scene, positioned so it is visible in the camera view when playing)
var prefabScoreText: GameObject;
I have also tried making the slot a Text$$anonymous$$esh type, and dragging in the prefab;
var prefabScoreText: Text$$anonymous$$esh;
And changing the setting code appropriatley (IE, remove the GetComponent and work with the prefabScoreText.text directly) But no joy.
The weird thing is, during runtime the rendered text does not change, but on returning to the editor the text has changed! (IE: the 3D text is attached to camera so it is in view. Before starting the game the camera view shows a big fat 0. after the game, it shows say, 160 - but in game it never changed from 0)
Answer by Owen-Reynolds · Dec 04, 2013 at 11:42 PM
You are probably changing the Asset (the real prefab, in Project) and not the actual gameObject. When you click on script variable prefabScoreText
in the Inspector, Unity should be pulsing the scoreText in the scene.
In general, if you plan to run Instantiate, the link should go to the "real" prefab, from the Project folder. If you plan to change values, the link should be to an already made object (and you almost never Instantiate and change the same thing.)
The terminology is a little confusing. The Asset, in the Project folder is the real prefab. When you drag it into a scene, you might refer to the new object as a "prefab chair." But, during play, it's just a regular gameObject. The magic link to the prefab is only to help during editing (which is why you aren't seeing your real score change.)
Ahh ok. I knew it was something along these lines just couldn't puzzle it out.
So, for the 3DText object, I have to instantiate that and child it to the camera at runtime? - because it's not possible to drag an instance from the scene hierarchy to the script slot - or am I missing something there?.
Yes you can drag and drop a game object from the scene hierarchy to the inspector and you don't need to instantiate it to correctly modify your object. But if you drag and drop a prefab, you need to instantiate it before changing something in it.
The only "can't drag" rule is you can't drag an instance from the scene into a script slot of a prefab. That rule makes sense, since a prefab can be spawned in any scene.
Cheers, altogether this helped me work it out.
I ended up with this in a script on the camera;
var HUDScorePrefab: GameObject;
@HideInInspector
var HUDScoreCloneText$$anonymous$$esh: Text$$anonymous$$esh;
function Start () {
var HUDScoreClone:GameObject=Instantiate(HUDScorePrefab,Vector3(0,0,0),Quaternion.identity);
HUDScoreClone.transform.parent=transform;
HUDScoreClone.transform.position=Vector3(-1.719547,0.8588534,0.8372908);
HUDScoreCloneText$$anonymous$$esh=HUDScoreClone.GetComponent(Text$$anonymous$$esh);
HUDScoreCloneText$$anonymous$$esh.text="0";
}
And this on the object that wants to update textmesh text;
function Start () {
//get a reference to the scoreboard
var mainCamera:GameObject=GameObject.Find("Shooting$$anonymous$$ovingCamera");
var scriptHUD$$anonymous$$anager:HUD$$anonymous$$anager=mainCamera.GetComponent(HUD$$anonymous$$anager);
HUDScore=scriptHUD$$anonymous$$anager.HUDScoreCloneText$$anonymous$$esh;
}
which then just uses HUDScore.text to set the text, and everything updates fine.
Your answer

Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Shooting at ground 1 Answer
How to make enemy AI with NavMesh 2 Answers
Climb ladder 2 Answers
C# Unity Script Examples & Question 2 Answers