Setting Text Mesh with a variable
I'm just starting unity and making a card game. I have a Quad GameObject "CardQuad" with a bunch of child objects, some of which are TextMesh objects that will represent the card name, abilities, health, and attack of the card. I am trying to set the TextMesh in Update(). I can set it to a quoted string, but when I try to set it to a string variable it comes out as blank when I hit play.
I've set my public variables in the UI.
Debug.Log shows:
"Rampaging Rhino of the Jungle
UnityEngine.Debug:Log(Object)
CardCreator:Start() (at Assets/CardCreator.cs:19)"
So I know my string assign is working.
Why is it not setting the TextMesh.text to my variable though?
using UnityEngine;
using System.Collections;
public class CardCreator : MonoBehaviour {
public string prefix;
public string suffix;
public string creatureType;
public int attack;
public int defense;
public string abilities;
public char color;
string cardName;
// Use this for initialization
void Start () {
cardName = prefix + " " + creatureType + " " + suffix;
Debug.Log (cardName);
}
// Update is called once per frame
void Update () {
GameObject g = GameObject.FindGameObjectWithTag ("CardName");
TextMesh t = g.GetComponent<TextMesh> ();
Debug.Log ("Name = '" + t.text + "'");
t.text = "some new string"; //this works and updates the card name
t.text = cardName; //this makes the card name blank
}
}
Hmmm... those should work the same. The only thing I can think of is maybe the length of the dynamic string is causing problems? Try making the hard-coded one really long and see if that has an effect.
Thank you, I did but it just spilled off the card. I set some more debug lines and it made me realize that Start() was being called more than once. $$anonymous$$y script was accidentally attached to a second GameObject in my project, causing the confusion.
Answer by dhaggerfin · Jun 08, 2016 at 07:43 PM
Updated by code and added more debug logs. Hopefully this can shed some light:
using UnityEngine;
using System.Collections;
public class CardCreator : MonoBehaviour {
public string prefix;
public string suffix;
public string creatureType;
public int attack;
public int defense;
public string abilities;
public char color;
string cardName;
// Use this for initialization
void Start () {
Debug.Log ("Card Name before Start = '" + cardName + "'");
cardName = prefix + " " + creatureType + " " + suffix;
GameObject g = GameObject.FindGameObjectWithTag ("CardName");
TextMesh t = g.GetComponent<TextMesh> ();
t.text = cardName;
Debug.Log ("Card Name after Start = '" + cardName + "'");
}
// Update is called once per frame
void Update () {
Debug.Log ("Card Name in Update = '" + cardName + "'");
}
}
Output in the console:
Seems like the variables set in the UI are losing their values. Also, Start is being called more than once? That doesn't seem right.
http://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html
Yes, that was it! $$anonymous$$y script was attached to another game object as well. This was clearing the values.
Your answer
![](https://koobas.hobune.stream/wayback/20220612082456im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why is my scene variable losing its value? 0 Answers
Why does this script require me to assign this variable twice to work? 1 Answer
How to change the player gravity level?,How to change the value of the players gravity? 1 Answer
Why is the private int variable in this c# program not updating? 1 Answer