[Solved] UI Text not updating C#

I’ve been having this problem for a while now but it only seems to happen in this project. When I try and use the textComponent.text property in Unity it doesn’t update the text. However, after I stop the game and play it again then the text I am trying to change changes to what I was trying to change before. For instance, if I am updating the score it won’t register the update in the text until I restart. Here are a couple of scripts that aren’t working.

using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityEngine.UI;
using System.Collections;

public class StoreManager : MonoBehaviour
	public bool touchingStore;
	public bool pressingKey;
	public bool hasItems = false;
	public Button btnBuy;
	public Button btnSteal;
	public Text GoldText;
	public GameObject MainCamera;
	public GameObject Canvas2;
	GameObject Child1;
	GameObject Child2;
	GameObject Child3;

	void Start ()
		touchingStore = false;
		pressingKey = false;

		GoldText = GetComponent<Text> ();

		print ("setting start text");
		GoldText.text = "25";

//		btnBuy.onClick.AddListener (BuyItems);
		btnSteal.onClick.AddListener (StealItems);

	void Update ()
		if (CrossPlatformInputManager.GetButtonDown ("EnterStore") == true) {
			pressingKey = true;

	void OnTriggerEnter2D (Collider2D coll)
		if (coll.gameObject.tag == "Store") {
			if (GameObject.Find ("Canvas2(Clone)") == null) {
				touchingStore = true;
				Instantiate (Canvas2);

	public void BuyItems ()
		print ("Changing text?");
		GoldText.text = "2";

	public void StealItems ()
		KillPlayerOnCollision.attack = true;
		Child1 = GameObject.Find ("Canvas2(Clone)").transform.GetChild (0).gameObject;
		Child1.SetActive (false);
		Child2 = GameObject.Find ("Canvas2(Clone)").transform.GetChild (1).gameObject;
		Child2.SetActive (false);
		Child3 = GameObject.Find ("Canvas2(Clone)").transform.GetChild (2).gameObject;
		Child3.SetActive (false);

Have you checked to see if you are changing a prefab instead of the object in the hierarchy?

If so then you can drag the prefab out of your project window into the heirarchy. You should now have a blue object in the heirarchy which is a copy of your prefab. Use that in your game instead of the prefab.

Delete the public in public Text GoldText;. Be very careful with public variables, can lead to very hard to find bugs where you think you are assigning a value in code but you also changed it in the inspector and it is serialised. I wish Unity didn’t use public to do this!

Note you are assigning GoldText with the line GoldText = GetComponent ();. This is fine if this script is actually on your Text object in the inspector. If it isn’t then this is your bug.

Note both of the above are assigning GoldText with values, one of them is very probably your bug.

Have you assigned the Text variable in the Inspector?
If so, then are you using TextVariable.text = "Your string here";?

I solved it by running it on Unity Main Thread.

UnityMainThreadDispatcher.Instance().Enqueue(() =>
        //your logic