- Home /
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();
}
}
Answer by Little_Gorilla · Feb 20, 2014 at 02:33 AM
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#.
Answer by Xicao · Jul 31, 2014 at 03:58 PM
Make sure you drag the GUIText from Hierarchy and not from Project Tab.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Pause Menu background problem 0 Answers
A simple problem with scripts 1 Answer
Trying to get score counter... 2 Answers
Script is not working at all 2 Answers