Update() function not working on Android.
I am trying to use the Update() function to every frame update all texts (as shown below). It works perfectly fine in the Game View, and it even works while using the Unity Remote app by connecting the Android device via USB; but when I build the game and runnit fom that same Android device the Update() function doesn's seem to be called. The function is inside a script which is attached as a Game Component to an empty object. Is there a way to make it work? Thanks!
Code:
void Update(){ //Update texts
GameObject.Find("Amount_Coal").GetComponent<UnityEngine.UI.Text>().text = Coal+ "/" + Space;
GameObject.Find("txtAmountCoins").GetComponent<UnityEngine.UI.Text>().text = Coins.ToString();
GameObject.Find("txt_PickaxePrice").GetComponent<UnityEngine.UI.Text>().text = ((int)Mathf.Round(Mathf.Pow(PickaxeLevel, 2) * 200) * (PickaxeLevel/25 + 1)).ToString();
GameObject.Find("txt_PriceInv").GetComponent<UnityEngine.UI.Text>().text = ((int)Mathf.Round(Mathf.Pow(LevelInv, 2) * 100) * (LevelInv/25 + 1)).ToString();
}
Answer by alanjoskowicz23 · Jan 16, 2021 at 04:44 AM
For anyone wanting to do what I explained in the question I found an answer. You should create a coroutine in which there is a while loop that keeps forever returning null. That coroutine must be called from an Awake() function. I tried it with Start() but when running it for the first time on mobile it didn't work (it did the following times) but it is solved with the Awake(). Code:
void Awake(){
StartCoroutine("UpdateTexts");
}
IEnumerator UpdateTexts(){
while (true){
GameObject.Find("Amount_Coal").GetComponent<UnityEngine.UI.Text>().text = Coal+ "/" + Space;
GameObject.Find("txtAmountCoins").GetComponent<UnityEngine.UI.Text>().text = Coins.ToString();
GameObject.Find("txt_PickaxePrice").GetComponent<UnityEngine.UI.Text>().text = ((int)Mathf.Round(Mathf.Pow(LevelPickaxe, 2) * 200) * (LevelPickaxe/25 + 1)).ToString();
GameObject.Find("txt_PriceInv").GetComponent<UnityEngine.UI.Text>().text = ((int)Mathf.Round(Mathf.Pow(LevelInv, 2) * 100) * (LevelInv/25 + 1)).ToString();
yield return null;
}
}