Create 3D text mesh using scripting instead in inspector

How should I create a 3D text mesh and placed it in the game scene, and the number inside text would change according to the script. This is what i had done so far.

public TextMesh SchoolText;
public TextMesh FarmText;

After I have declared these 2 textmesh, I am able to edit the text in the text mesh in the inspector, but I want to do it inside a script instead of using the inspector to input my text. How should I use code it such as I would edit the text using scripting instead of using the inspector to edit my text.

You can use TextMesh.text to edit the content of the TextMesh.

public TextMesh myTextMesh;

void Start()
{
myTextMesh.text = "text";
}

If you mean creating a TextMesh dynamically and assigning the values via scripting, you’ll need to use AddComponent to add the TextMesh to a game object. After which you’ll have to assign the font, material and finally set the text using TextMesh.text.

Ya, it is able to work but what should I do if I need to change the textmesh number using scripting. For example, i am creating a Health Point for my gameObject , then depend on the game situation, the HP would increase or decrease on the game. How should I code it such as this?

When you change your health variable, just inform the script to update your TextMesh as well.

void DecreaseHealth(int damage)
{
    health -= damage;
    healthText.text = health.ToString();
}

What I am doing now.

GUIScript scr = GetComponent<GUIScript>();
GameObject obj = GameObject.Find ("SchoolHitpoint");
SchoolText.text = scr.SchoolHitpoint.ToString();
SchoolText.text = "HP:      /1000";

Right now, the code is working but the number is not shown on the screen, instead i can only see the hard code HP: /1000 on my game scene,is there anything wrong with my code?

You’re overriding the actual health value with your other text. What you want to do is

SchoolText.text = "HP: " + scr.SchoolHitpoint.ToString() + "/1000";

Also, it’s recommended that you do not call GameObject.Find too many times. It’s bad for performance. Call it once in Start and store the result in a variable in the script.

Ya, thank for your help, but I got another problem. Now I could see the SchoolHitpoint variable number, but not sure why when I press ‘p’ button, the SchoolHitpoint variable does not change the number at all but instead the number is the same.Here is my update code.

void update()
{
if(Input.GetKey(KeyCode.P))
		{
			GUIScript scr = GetComponent<GUIScript>();
			GameObject obj = GameObject.Find ("SchoolHitpoint");
			scr.SchoolHitpoint -= 500;
			
		}
}

You’ll need to update your TextMesh when you update your hitpoints.

GameObject obj;
public TextMesh schoolText;

void Start()
{
	obj = GameObject.Find("SchoolHitpoint");
}

void Update()
{
	if(Input.GetKey(KeyCode.P))
	{

		GUIScript scr = GetComponent<GUIScript>();
		scr.SchoolHitpoint -= 500;
		schoolText.text = "HP: " + scr.SchoolHitpoint.ToString() + "/1000";
	}
}

Also, as I said before, it’s not recommended to keep calling GameObject.Find in Update.

Hmm ok, i trying to do the way like u did, but I dun know why when I run the game, it give me a very random number -12 500/ 1 000, this is my code

void Start()

{
    GameObject obj = GameObject.Find ("SchoolHitpoint");
    obj = GameObject.Find("SchoolHitpoint");

}

 

void Update()

{

    if(Input.GetKey(KeyCode.P))

    {

 

        GUIScript scr = GetComponent<GUIScript>();

        scr.SchoolHitpoint -= 500;

        schoolText.text = "HP: " + scr.SchoolHitpoint.ToString() + "/1000";

    }

}

I double check my variable in another script, it is correct which is 1000, dun know why I get this random number.

Were you holding down P when it happened? GetKey is true as long as the key is down. If you only want to press once, you’ll need to use GetKeyDown. Are you absolutely sure that scr.SchoolHitpoint is not giving a negative number?
Also, why are you doing the GameObject.Find twice in Start? I have already declared obj variable as a private variable for the script. You don’t have to call GameObject.Find again. Also, that should have given you an error. You can’t declare another variable named obj in the script.

I change it to GetKeyDown, now I get another random number for unknown reason. And I never touch the ‘p’ button at all when I create the HP.

1251790--54268--$qwerty.jpg

Something else must be influencing the SchoolHitpoint variable. Is that input statement the only thing changing the SchoolHitpoint variable? Is there anything else that is using the SchoolHitpoint variable?

no at all, I dun know why I get this specify random number, -20 500, when i press ‘p’ button, it do nothing.

Can you show me what’s in your GUIScript script?

public int SchoolHitpoint= 1000;

This is the only code that I have create the SchoolHitpoint variable in GUIScript.cs. Checked using ctrl +f

When you disable the line that decreases the SchoolHitpoint variable, does it still print a negative number?

Hmm, i think i find out the problem, I try change the SchoolHitpoint in GUIScript.cs to 8000, in the end when I test out the program, i get 2000/2000 and no more negative number when I removed the line that decreases the SchoolHitpoint. I view the inspector and removed the info there which is " HP: /2000 that I have wrote in the inspector, and run the program again, it would not print anything. I suspect it is the inspector causing the problem or?

If I’m not wrong, someone mentioned that public variables get their values from the inspector. What I would do is that I would override the public variable value in Start.

public int hitPoint;
void Start()
{
    hitPoint = 1000; // Ensures hitPoint is always 1000 at the Start
}

There’s no harm trying it. It’s a really strange bug. I have never encountered it when I use TextMesh to display values like health and such.

Ya, the problem now is it would get the values from the inspector text mesh instead of my script text mesh. If I removed the values from the inspector text mesh and write nothing, the program would print nothing too. I tried to declare hP of the school in the start function, but if i removed the info in the inspector and run the program, it would print nothing too.

If you override the text in the TextMesh, the text will change on the TextMesh. The only problem now is why your SchoolHitpoint variable is changing its value. What you can try to do is to override it in Start and see if overriding the text with the SchoolHitpoint variable will give you the expected result. Try commenting out all the lines that are changing the SchoolHitpoint value.

Your TextMesh should display the right value. Or at least whatever SchoolHitpoint value is. If it is not displaying the right value, maybe it’s time to debug it to see what is changing the value?