Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

unity 4.6 UI "Text" could not bet found?

Discussion in '2D' started by GreenTee, Jan 2, 2015.

  1. GreenTee

    GreenTee

    Joined:
    Jul 25, 2014
    Posts:
    11
    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".

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ScoreManager : MonoBehaviour
    5. {
    6.     public static int score;        // The player's score.
    7.  
    8.  
    9.     Text text;                      // Reference to the Text component.
    10.                                          //Text is red
    11.  
    12.     void Awake ()
    13.     {
    14.         // Set up the reference.
    15.         text = GetComponent <Text> (); // Text is red
    16.      
    17.         // Reset the score.
    18.         score = 0;
    19.     }
    20.  
    21.  
    22.     void Update ()
    23.     {
    24.         // Set the displayed text to be the word "Score" followed by the score value.
    25.         text.text = "Score: " + score; //  secound "text" is red
    26.     }
    27. }
    I hope anyone knows what to do :)



    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?
     
    zrt56 and ContractorNation like this.
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    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:

    Code (csharp):
    1. using UnityEngine.UI;
     
  3. GreenTee

    GreenTee

    Joined:
    Jul 25, 2014
    Posts:
    11
    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.
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Could you show the updated script?
     
    GreenTee likes this.
  5. GreenTee

    GreenTee

    Joined:
    Jul 25, 2014
    Posts:
    11
    new script for displaying the score and a function to add points:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI
    3. using System.Collections;
    4.  
    5. public class ScoreManager : MonoBehaviour
    6. {
    7.     public static int score = 0;        // The player's score.
    8.     Text text;                      // Reference to the Text component. "Text" red
    9.  
    10.     void Awake ()
    11.     {
    12.         // Set up the reference.
    13.         text = GetComponent <Text> (); // "Text" red
    14.         // Reset the score.
    15.         score = 0;
    16.     }
    17.  
    18.     void Update ()
    19.     {
    20.         // Set the displayed text to be the word "Score" followed by the score value.
    21.         text.text = "Score: " + score; // secound text red
    22.     }
    23.  
    24.     static public void AddPoint(){  //Use script to add point
    25.         score++;
    26.  
    27.         }
    28. }
    29.  
    30.  
    i thought to add an other script to an collider to add the score, but it has a error too:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ScoreAddingScript : MonoBehaviour {
    5.  
    6.     void OnTriggerEnter2D(Collider2D collider) {
    7.             if (collider.tag == "Player") {
    8.             Score.AddPoint();
    9.             gameObject.SetActive(false);
    10.                 }
    11.         }
    12. }
    13.  
     
    Last edited: Jan 5, 2015
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You forgot the semicolon at the end of the second line.
     
    kenzichinta and GreenTee like this.
  7. GreenTee

    GreenTee

    Joined:
    Jul 25, 2014
    Posts:
    11
    .... i am stupid :D I thank you alot.
     
  8. GreenTee

    GreenTee

    Joined:
    Jul 25, 2014
    Posts:
    11
    Hello again! I have a small problem. What i have to do if "Score : 0" does not count?

    ScoreAddingScript

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ScoreAddingScript : MonoBehaviour {
    5.  
    6.     void OnTriggerEnter2D(Collider2D collider) {
    7.             if (collider.tag == "Player") {
    8.             ScoreManager.AddPoint();
    9.             //gameObject.SetActive(false);
    10.  
    11.  
    12.             Debug.LogError ("Activ ontrigger");
    13.                 }
    14.         }
    15. }
    16.  
    ScoreManager scirpt

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class ScoreManager : MonoBehaviour
    6. {
    7.     public static int score = 0;// The player's score.
    8.     Text text;                      // Reference to the Text component.
    9.    
    10.     void Awake ()
    11.     {
    12.         // Set up the reference.
    13.         text = GetComponent <Text> ();
    14.         // Reset the score.
    15.         score = 0;
    16.     }
    17.  
    18.     void Update ()
    19.     {
    20.         // Set the displayed text to be the word "Score" followed by the score value.
    21.         text.text = "Score: " + score;
    22.  
    23.     }
    24.  
    25.     static public void AddPoint(){
    26.         score += score +1;
    27.  
    28.         Debug.LogError ("adding Points");
    29.         }
    30. }
    Everything is working except displaying the Score with the text UI.
     
  9. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Line 26 doesn't seem correct. It should be either one of these:

    Code (CSharp):
    1. score = score + 1;
    2.  
    3. score += 1;
     
    djafar12345 likes this.
  10. Kanish_Ravikumar_

    Kanish_Ravikumar_

    Joined:
    Jun 13, 2021
    Posts:
    1
    Thank You So Much