[SOLVED] GetComponent<GUIText>().text not showing

Hi,

I was following a tutorial on making a scorelist and I tried to modify it so it could work for up to 4 players.
But when I start the game the text isn’t showing. although the code gets executed, I can tell by the console.
(video) https://i.gyazo.com/ebd7a6fa33b34730d095a631b4ad7178.mp4
Here’s how the score object is set up:


This is the score script that should change and display the score (console part is working):

using UnityEngine;
using System.Collections;

public class Score : MonoBehaviour
{
    public int score = 0;                    // The player's score.
    private int previousScore = 0;            // The score in the previous frame.

    void Update ()
    {
 
        // Set the score text.
        GetComponent<GUIText>().text =  score + " kills";

        // Set the previous score to this frame's score.
        previousScore = score;

        Debug.Log("SCORE: " + score);
    }

}

This is how I can the referenced score objects:

using UnityEngine;
using System.Collections;

public class ShotPlayer : MonoBehaviour {
    private int playerNum;
    private Score score1;
    private Score score2;
    private Score score3;
    private Score score4;

    void Awake()
    {
        //Setting up the referenses.
        score1 = GameObject.Find("p1_score").GetComponent<Score>();
        score2 = GameObject.Find("p2_score").GetComponent<Score>();
        score3 = GameObject.Find("p3_score").GetComponent<Score>();
        score4 = GameObject.Find("p4_score").GetComponent<Score>();
    }

    public void Hurt(int playerWhoShotMe)
    {
        playerNum = playerWhoShotMe;
        Debug.Log("I got shot by: " + playerNum);

        if (playerNum == 1)
            score1.score += 1;
        else if (playerNum == 2)
            score2.score += 1;
        else if (playerNum == 3)
            score3.score += 1;
        else if (playerNum == 4)
            score4.score += 1;
    }
}

So I’m missing something and can’t seem to find out what… Been looking at it for roughly 2 hours, maybe a new set of eyes can sort this out.

Thanks allot guys!

Put a “using UnityEngine.UI;” in the code

GUIText is from the old system. If you are using canvas you need the UI namespace as above and the class is just “Text”.

Be aware that there was a change in the UI systems in unity version 4.6, a lot of tutorials will be for the older system; newer system is much better :slight_smile:

Exactly what I said, what a coincidence :stuck_out_tongue:

These versions are making me super confused xD need to pay more attention from were I get my information I think.
LeftyRighty, already told you you are a genius, but we have a new magestic creature with us, thank you both for solving this :slight_smile:

Here’s the result: (video) https://i.gyazo.com/e7c9437870fe1c66c834e1eaabcf3445.mp4

adding the namespace wouldn’t make GUIText work, it requires both bits, the namespace and the correct class name. Wanted it to read as a “what he said and this bit” rather than a contradictory post that said “do it this way”… maybe that didn’t come across.

1 Like

Sorry about that, I didn’t pay attention to the wrong classname

1 Like

thank you so much.