- Home /
Why if I execute .SetText on the Start it dont works.
Hello, I want to execute in the Start of the script to set a text in to "texto", but when I execute it on Start dont works. If I use execute it on the Update it works but its no in the beginning, the update will send the messages 60frames x sec.
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class ShowZenis : MonoBehaviour
{
// Start is called before the first frame update
private ZeniManager zm;
public TextMeshProUGUI texto;
public int dinero;
void Awake()
{
zm = gameObject.AddComponent<ZeniManager>(); //Instanciamos un nuevo objeto, en MonoBehaviour se instancian así.
texto = (GameObject.Find("Monedas").GetComponent<TextMeshProUGUI>());
}
// Update es llamado una vez por cada frame.
void Start(){
dinero = zm.getZenis();
texto.SetText("" + dinero);
}
public void OnClickIt() {
if(gameObject.name == "ButtonAdd") {
zm.addMoney(40);
dinero = zm.getZenis();
texto.SetText("" + dinero);
//PlayerPrefs.DeleteAll();
}
}
}
IF I USE START:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class ShowZenis : MonoBehaviour
{
// Start is called before the first frame update
private ZeniManager zm;
public TextMeshProUGUI texto;
public int dinero;
void Awake()
{
zm = gameObject.AddComponent<ZeniManager>(); //Instanciamos un nuevo objeto, en MonoBehaviour se instancian así.
texto = (GameObject.Find("Monedas").GetComponent<TextMeshProUGUI>());
}
// Update es llamado una vez por cada frame.
void Update(){
dinero = zm.getZenis();
texto.SetText("" + dinero);
}
public void OnClickIt() {
if(gameObject.name == "ButtonAdd") {
zm.addMoney(40);
dinero = zm.getZenis();
texto.SetText("" + dinero);
//PlayerPrefs.DeleteAll();
}
}
}
IF I USE UPDATE:
You will say me: use the Update, but the problem is when I use the Update I cant modify the text, like its forever don't know how to say it.
Thanks.
Have you tried texto.text = "" + dinero;
ins$$anonymous$$d of texto.SetText("" + dinero);
?
Answer by Statement · Dec 20, 2019 at 02:36 PM
Perhaps something else sets the text of texto after you have set it, overriding your text. I don't recall if you could change the Script Execution Order of Start calls.
Also you could do an if (!loadedTexto) { loadedTexto = true; texto.SetText("" + dinero); }
where you set the value once in Update. A bit of a hack, but should do it.
I changed my comment to an answer and marked it as accepted for you.
Please unaccept if you feel there is a better answer.