unity 4.6 UI "Text" could not bet found?

Hello Com. ,

i want to use the score script written by the Unity team for a game but Unity can not find the type or namespace name “Text”.

using UnityEngine;
using System.Collections;

public class ScoreManager : MonoBehaviour
{
    public static int score;        // The player's score.
 
 
    Text text;                      // Reference to the Text component.
                                         //Text is red
 
    void Awake ()
    {
        // Set up the reference.
        text = GetComponent <Text> (); // Text is red
     
        // Reset the score.
        score = 0;
    }
 
 
    void Update ()
    {
        // Set the displayed text to be the word "Score" followed by the score value.
        text.text = "Score: " + score; //  secound "text" is red
    }
}

I hope anyone knows what to do :slight_smile:

ERROR message:

Assets/Scripts/Score.cs(9,9): error CS0246: The type or namespace name `Text’ could not be found. Are you missing a using directive or an assembly reference?

2 Likes

Text is in the UnityEngine.UI namespace. That means you need to either use UnityEngine.UI.Text or you have to include it at the beginning of the file:

using UnityEngine.UI;
9 Likes

i added the script part at the top but now i have a new error :

Assets/Scripts/Score.cs(3,1): error CS1041: Identifier expected: `using’ is a keyword

And the the “Text” is still red/an error.

Could you show the updated script?

1 Like

new script for displaying the score and a function to add points:

using UnityEngine;
using UnityEngine.UI
using System.Collections;

public class ScoreManager : MonoBehaviour
{
    public static int score = 0;        // The player's score.
    Text text;                      // Reference to the Text component. "Text" red

    void Awake ()
    {
        // Set up the reference.
        text = GetComponent <Text> (); // "Text" red
        // Reset the score.
        score = 0;
    }

    void Update ()
    {
        // Set the displayed text to be the word "Score" followed by the score value.
        text.text = "Score: " + score; // secound text red
    }

    static public void AddPoint(){  //Use script to add point
        score++;

        }
}

i thought to add an other script to an collider to add the score, but it has a error too:

using UnityEngine;
using System.Collections;

public class ScoreAddingScript : MonoBehaviour {

    void OnTriggerEnter2D(Collider2D collider) {
            if (collider.tag == "Player") {
            Score.AddPoint(); 
            gameObject.SetActive(false);
                }
        }
}

You forgot the semicolon at the end of the second line.

2 Likes

… i am stupid :smile: I thank you alot.

Hello again! I have a small problem. What i have to do if “Score : 0” does not count?

ScoreAddingScript

using UnityEngine;
using System.Collections;

public class ScoreAddingScript : MonoBehaviour {

    void OnTriggerEnter2D(Collider2D collider) {
            if (collider.tag == "Player") {
            ScoreManager.AddPoint();
            //gameObject.SetActive(false);


            Debug.LogError ("Activ ontrigger");
                }
        }
}

ScoreManager scirpt

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ScoreManager : MonoBehaviour
{
    public static int score = 0;// The player's score.
    Text text;                      // Reference to the Text component.
   
    void Awake ()
    {
        // Set up the reference.
        text = GetComponent <Text> ();
        // Reset the score.
        score = 0;
    }

    void Update ()
    {
        // Set the displayed text to be the word "Score" followed by the score value.
        text.text = "Score: " + score;

    }

    static public void AddPoint(){
        score += score +1;

        Debug.LogError ("adding Points");
        }
}

Everything is working except displaying the Score with the text UI.

Line 26 doesn’t seem correct. It should be either one of these:

score = score + 1;

score += 1;
1 Like

Thank You So Much