- Home /
how do I fix my text box not updating?
I have recently been trying to make a clicker game, and obviously in a clicker game you need some sort of score counter that shows how much of whatever your currency is. I wrote a block counter script that adds 1 to the score every time you click and it works. my text box changes from a score of 0 to 1 when i click then goes to 2 and so on as i click. I tried to make an auto score counter code that I can apply to upgrades. These next set of codes are what I use for a upgrade I called "Miner"
Code 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MinerCode : MonoBehaviour
{
public GameObject FakeButton;
public GameObject FakeText;
public GameObject RealButton;
public GameObject RealText;
public int ScoreValue;
public static int MinerValue = 20;
public static bool TurnOffButton = false;
public GameObject MinerStats;
public static int MinerAmount;
public static int MinesPerSecond;
// Start is called before the first frame update
void Start()
{
RealButton.SetActive(false);
}
// Update is called once per frame
void Update()
{
ScoreValue = BlockCounter.ScoreValue;
MinerStats.GetComponent<Text>().text = "MINER: " + MinerAmount + " AT " + MinesPerSecond + " BPS";
FakeText.GetComponent<Text>().text = "MINER - Unaffordable ( " + MinerValue + " Blocks)";
RealText.GetComponent<Text>().text = "MINER - " + MinerValue + " Blocks";
if (ScoreValue >= MinerValue)
{
FakeButton.SetActive(false);
RealButton.SetActive(true);
}
if (TurnOffButton == true)
{
RealButton.SetActive(false);
FakeButton.SetActive(true);
TurnOffButton = false;
}
}
}
Code 2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AutoBlockMiner : MonoBehaviour
{
public bool CreatingBlocks = false;
public static int BlockIncrease = 5;
public int InternalIncrease;
// Update is called once per frame
void Update()
{
BlockIncrease = MinerCode.MinesPerSecond;
InternalIncrease = BlockIncrease;
if (CreatingBlocks == false)
{
CreatingBlocks = true;
StartCoroutine(CreateTheBlock());
}
}
IEnumerator CreateTheBlock()
{
BlockCounter.ScoreValue += InternalIncrease;
yield return new WaitForSeconds(1);
CreatingBlocks = false;
}
}
Code 3
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PurchaseLogMiner : MonoBehaviour
{
public GameObject AutoBlock;
public AudioSource PlayMinerSound;
public void StartAutoBlock()
{
PlayMinerSound.Play();
AutoBlock.SetActive(true);
BlockCounter.ScoreValue -= MinerCode.MinerValue;
MinerCode.MinerValue *= 2;
MinerCode.TurnOffButton = true;
MinerCode.MinesPerSecond += 5;
MinerCode.MinerAmount += 1;
}
}
When I try to use these codes and apply them to unity, it appears to be fine, but when I play the game, the score value doesnt increase by 5 ( I used 5 because that is how much the miner is supposed to give every second when bought) until I click. In the inspector however, I can clearly see the score increase with the InternalIncrease int. How can I make this so the text box updates even when I dont click my button?
Whenever you change the value you should update the text component, but you should also reference them as Text objects not GameObjects so you don't have to use GetComponent, it uses a lot of resources.
hey there, thank you for the reply, when i get the chance Ill do this. What code would I need to add to make it update the text component?
Answer by joan_stark · Aug 24, 2021 at 08:36 PM
Well, first, make surte cache all your text components. You can do that by
public class MinerCode : MonoBehaviour
{
public GameObject FakeButton;
public GameObject FakeText;
[...]
//I will do minerstats text only for example.
private Text minerStatsText;
void Start()
{
RealButton.SetActive(false);
//Here we set minerStatsTextComponent:
minerStatsText = MinerStats.GetComponent<Text>();
}
void Update()
{
ScoreValue = BlockCounter.ScoreValue;
//Now here you do:
minerStatsText.text = "MINER: " + MinerAmount + " AT " + MinesPerSecond + " BPS";
[...]
}
}
And well, about the update text not working, you are not changing any time the values of Miner Amount or Mines per second, for example. You only do it once at most in StartAutoBlock. When you create a StartAutoBlock, you should add and update method that updated the value every frame, or every second in order to make it change.
Your answer
Follow this Question
Related Questions
Input Problems 2 Answers
How to stop code in the update function? 1 Answer
Coroutine throught update funct 0 Answers
Weird Spring Oscillation Error 1 Answer