GuiText not updating

I’m having trouble getting my score counter show up properly on screen. I am trying to use GuiText and I have it tied into a score script. I want it to update when the Player kills an Enemy. I can tell by the console that the score variable is incrimenting properly, but the GuiText is not updating.

Here are the two scripts:

JumpDeath.cs

using UnityEngine;
using System.Collections;

public class JumpDeath : MonoBehaviour {
	public GameObject deathInstance = null;
	public Vector2 deathInstanceOffset = new Vector2(0,0);
	public Score scoreScript;

	void OnTriggerEnter2D (Collider2D other)
	{
		if (other.gameObject.tag == "Player") 
		{
			OnKill();
		}
	}
	


	void OnKill()
	{
		if (deathInstance) {
			var pos = gameObject.transform.position;
			GameObject clone = Instantiate (deathInstance, 
			                                new Vector3 (pos.x + deathInstanceOffset.x, 
			             pos.y + deathInstanceOffset.y, 
			             pos.z), Quaternion.identity) as GameObject;

			scoreScript.AddScore();
		}
		print (scoreScript.score.ToString ());
		Destroy(gameObject);
	}

}

Score.cs

using UnityEngine;
using System.Collections;

public class Score : MonoBehaviour {
	public int score;


	// Use this for initialization
	public void Start () {


	}

	public void AddScore(){
		score += 1;

	}

	// Update is called once per frame
	void Update () {
		guiText.text = score.ToString();
	}
	
}

2 Answers

2

You have to declare the GuiText in Score.cs and then drag the GuiText GameObject over in the inspector

public GUIText guiText;

I have attempted this linking, but it does not appear to have changed the effect. The text is still not updating. I am not clear how establishing this relationship would help in this case, I am very new to C#.

–

Make sure you drag the GUIText from Hierarchy and not from Project Tab.