I tried to display the text through

Void OnGUI(){GUI.Label(new Rect(330, 120, 100, 20), count.ToString());} 

The text is displayed. But I would like to through the UI, in a new, in a beautiful

I watched the video (off. Manual Unity by UI text) https://unity3d.com/ru/learn/tutorials/topics/user-interface-ui/ui-text

There is a separate ScoreManager.cs file. There is an Awake method.

How can I put my text from my script in this UI text where there is no Awake method? Tried to put the code in the Start method, does not display.

  • And how did you try? can I see the code? And the script with the given code is on what object? On the UI text object or on some other? ....... and yes ... you can always write the Awake method yourself, nobody forbids you) - Alexey Shimansky

2 answers 2

Now in Unity there are at least three standard ways to display text on the screen:

  • old UI (as you specified via OnGUI ())

  • new ui

  • via TextMesh (better to use when you need to display text in the game space, rather than via UI)

If I understand the question correctly, then you need the Text component in the new UI. Then an option for an example:

  1. We add in the hierarchy of the scene a new UI-> Text
  2. In the necessary script, add the field public Text MyTestLabel;
  3. The script must be added to any element in the hierarchy (for example, some kind of scene controller, or even a UI object itself, it depends on your architecture).
  4. In the scene element inspector, to which you connected the script, drag an element into the appeared field (via the scene hierarchy)
  5. For example, in the Start () method, add the line MyTestLabel.text = "Hello world";

The position on the screen for the Text component will be set in the editor.

Try adding a Debug.Log in the Start () method (the name of the variable, the value you want to display); It is possible that at this moment the desired value is not ready. Transfer to Update, for example. Look at it, what is the value of the variable

In addition, if the script is inherited from the Monobehaviour class, then all standard functions such as Start (), Update (), etc. available for definition.

  • It is not dragged! What I do is create through the menu GameObject-> UI-> Text, create a text frame with the inscription inside "New Text". Then create in the script after MonoBehavior public Text tt; And in start () tt.text = "testik"; In the properties of the script there is a place to drag, None (Text) all okay. But the text itself from the hierarchy is not dragged there, just a prohibitory sign stands. - Dmitrii
  • Strange. Attach a screenshot. It sounds like the editor doesn’t like the type of item being dragged. - DarkOwl
  • Alternatively, try (for a temporary test) to make a GameObject variable. And then in the private void Start () method write Debug.Log ("is the object null equal?" + The name of your_variable.gameobject.GetComponent <Text> () == null); and then move your UI component to the new field. If transferred, then run and check what output to the console. - DarkOwl
  • public class Snake2 : MonoBehaviour { public Text tt; } void Start() { tt.text = "testik"; InvokeRepeating("Move", 0.3f, 0.3f); } public class Snake2 : MonoBehaviour { public Text tt; } void Start() { tt.text = "testik"; InvokeRepeating("Move", 0.3f, 0.3f); } And the screenshot is the link , I hope, nothing is broken. - Dmitrii
  • one
    It seems I understood what was the matter. The script must be attached to the object on the stage. And even then, in the scene object inspector, you need to drag items. I will add this item just in case in response, too. When you yourself know what and how, it is sometimes hard to describe all the details, because for the most obvious. (I added the answer, I hope it became clearer) - DarkOwl

What script? You need to get a pointer to the buttons or their text and then change. It depends on what your script looks like.

http://docs.unity3d.com/en/current/Manual/ControllingGameObjectsComponents.html