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

How can I make GUIText work?

Discussion in 'Scripting' started by Findev, Dec 23, 2015.

  1. Findev

    Findev

    Joined:
    Apr 17, 2015
    Posts:
    34
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class LevelUp : MonoBehaviour {
    4.     int LevelyUp = 0;
    5.     void OnClick(GameObject a) {
    6.         LevelyUp++;
    7.         UnityEngine.GUIText.text = "You have this many xp points: " + LevelyUp.ToString() + "$";
    8.     }
    9. }
    I want GUIText to say something like "You have this many xp points: " + LevelyUp.ToString() + "$";
    I tried a few methods before, but they didn't seem to work. I use Unity 5.1, by the way.

    Error:
    Assets/LevelUp.cs(5,24): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.GetComponent(System.Type)'
     
    Last edited: Dec 23, 2015
  2. kietus

    kietus

    Joined:
    Jun 4, 2013
    Posts:
    54
    Hello,

    GUIText is a class, and "text" is not a static member. Only method or properties declared as static can be used directly on the class.
    An instantiated object is needed to use the .text.
    You should have a GUIText component attached to your LevelUp object.
    You can
    Code (csharp):
    1.  
    2. // declare a var to hold the component ref
    3. private GUIText textUI;
    4. // initialize your component in awake
    5. void Awake(){
    6.     textUI = GetComponent<GUIText>(); // assuming there a GUIText component attached
    7. }
    8.  
    9. // now you can change the text
    10. void OnClick(GameObject a) {
    11.         LevelyUp++;
    12.         textUI.text = "You have this many xp points: " + LevelyUp.ToString() + "$";
    13.         Debug.Log(textUI.text); // You can call the Log function directly on Debug class because the Log function is static.
    14. }
    15.  
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You should also stop using GUIText
    There's like 4 different ways to do text in-game, and you picked the oldest and least convenient option
     
    Kiwasi likes this.
  4. Findev

    Findev

    Joined:
    Apr 17, 2015
    Posts:
    34
    What component should I use?
     
  5. kietus

    kietus

    Joined:
    Jun 4, 2013
    Posts:
    54
    Kiwasi likes this.
  6. Findev

    Findev

    Joined:
    Apr 17, 2015
    Posts:
    34
  7. Findev

    Findev

    Joined:
    Apr 17, 2015
    Posts:
    34
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public class LevelUp : MonoBehaviour {
    5.     int LevelyUp = 0;
    6.  
    7.     // declare a var to hold the component ref
    8.     private GUIText textUI;
    9.     // initialize your component in awake
    10.     void Awake(){
    11.         textUI = GetComponent<GUIText>(); // assuming there a GUIText component attached
    12.     }
    13.  
    14.     // now you can change the text
    15.     void OnClick(GameObject a) {
    16.         LevelyUp++;
    17.         textUI.text = "You have this many xp points: " + LevelyUp();
    18.         Debug.Log(textUI.text); // You can call the Log function directly on Debug class because the Log function is static.
    19.     }
    20. }
    Two errors when I use UI text:
    Assets/LevelUp.cs(9,70): error CS0019: Operator `+' cannot be applied to operands of type `string' and `method group'
    Assets/LevelUp.cs(10,42): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected

    For the first error, I would like to convert some GUIText to UI.Text

    The reason why I'm changing to UI.Text is because GUIText didn't show "You have this many xp points:" and the int LevelyUp.
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Remove the brackets on line 17. Variables do not need to be invoked.
     
  9. Findev

    Findev

    Joined:
    Apr 17, 2015
    Posts:
    34
    No errors :D, but the text doesn't show up
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public class LevelUp : MonoBehaviour {
    5.     int LevelyUp = 0;
    6.    
    7.     // declare a var to hold the component ref
    8.     private GUIText textUI;
    9.     // initialize your component in awake
    10.     void Awake(){
    11.         textUI = GetComponent<GUIText>(); // assuming there a GUIText component attached
    12.     }
    13.    
    14.     // now you can change the text
    15.     void OnClick(GameObject a) {
    16.         LevelyUp++;
    17.         textUI.text = "You have this many xp points: " + LevelyUp;
    18.         Debug.Log(textUI.text); // You can call the Log function directly on Debug class because the Log function is static.
    19.     }
    20. }
     
  10. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    What is OnClick
    theres no such method called automatically
    maybe you mean OnMouseDown