- Home /
Text is not updating
Hi! My problem is, text is not updating. I have prefab, and when game start, and you click a button he appears and you need to click it. I can't set a text to prefab , so they show score. Can you help me? P.S. sorry for my english. Here's code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using System;
public class click : MonoBehaviour
{
public int score;
[SerializeField]private TextMesh scoreText;
private void Awake()
{
scoreText.GetComponent<TextMesh>();
}
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);
if (hit)
{
score++;
}
}
scoreText.text = score.ToString();
}
}
In your Awake() method try to write
scoreText = scoreText.GetComponent<TextMesh>();
Do you have any error in the console at runtime? Maybe a NullReferenceException? In the latter case, it either means you haven't provided a gameObject in the scoreText
field in the inspector or your ray does not hit anything.
Note that the line you have in Awake does not do anything. You may want this instead, supposing you have a TextMesh
component attached to the same object as your click
component:
private void Awake()
{
scoreText = GetComponent<TextMesh>();
}
No, the Raycast works, i get point if click, but i cant set text to prefab. When i tried to do text prefab and set it the points was writing in prefab but not in text in game.
Answer by Statement · Dec 14, 2021 at 03:57 PM
Works fine for me. Make sure you actually have a TextMesh component in your scene/gameObject and not a Text or TextMeshProUGUI component. Those are not TextMeshes.
private void Awake()
{
// Grab MeshText from this gameObject if not set in inspector
if (!scoreText)
scoreText = GetComponent<TextMesh>();
// Log an error if there is no TextMesh
if (!scoreText)
Debug.LogError("No TextMesh set in inspector or attached to this game object", gameObject);
}
The only other thing I can think of is if you have another script somewhere setting the text of the TextMesh after your script has set the score value.
No it’s textmesh, but I can’t set it to prefab because text in scene and the object is prefab. I tried to do text like prefab ,and then spawn it, and then set it but in this case the score write in text prefab. I don’t know how I do at the text ugdating.
So the game is like you click the butto and the the button spawns and you click it.
After you Instantiate the prefab, set it up with code like using GetComponent. That, or search for the game object with the TextMesh with GameObject.Find during Start or Awake.
Your answer
Follow this Question
Related Questions
How to change UI button of a particular gameobject to another UI button 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Help setting up a game manager 1 Answer
To see chosen random text UI Unity,To Access UI text 0 Answers