I want to display the number of points and the amount of life of the player using the class GUI.Label . Part of this code looks like this:

 void onGUI() { GUI.Label (new Rect (10, 10, 60, 20), "Score: " + player.score.ToString()); GUI.Label (new Rect (10, 30, 60, 20), "Score: " + player.lives.ToString()); } 


But after the launch of the project these labels are not. Also, I tried to create a new project and copy the code that is specified in the official certificate. There is also nothing shown. Any ideas?
Unity3D 5.2.1f1
https://drive.google.com/file/d/0By40bSw4WlDGNi1TT2hFdThnNWM/view?usp=sharing
Addition:
Even if you put this code from the official certificate with multi-colored buttons into the new project, there is nothing after the launch:

 void OnGUI() { GUI.color = Color.yellow; GUI.Label(new Rect(10, 10, 100, 20), "Hello World!"); GUI.Box(new Rect(10, 50, 50, 50), "A BOX"); GUI.Button(new Rect(10, 110, 70, 30), "A button"); } 


Addition:
In general, Paul's comment helped me out. It was necessary to add a script to the camera. But the fun is that apparently in previous versions of Unity this was not necessary. Therefore, most theoretical courses contain this onGUI method code in those scripts that are responsible, for example, for the player, and the script could be located on a completely different object.
PS Moderators, how to make a comment the right answer?

  • Exactly not, or displays 5 font, which is slightly visible? - pavel
  • He is definitely not there. I made several attempts. Both on Mac and on win pc. On empty projects. Labels for some reason do not appear. - Amateur
  • one
    Quite a stupid question, is the script exactly on the camera hanging? - pavel
  • 2
    And yes, gui only at startup is shown, not in a preview. - pavel

1 answer 1

Starting with version 4.6, Unity 3D uses a new UI system with the ability to edit the interface in WYSIWYG mode. You no longer need to work with the interface in onGUI, because the event system is introduced.

First you need to add Ui-> Canvas to the project, then hang the Text component on it and bind the script to this component:

 private void Start() { Text mytext= GetComponent<Text>(); mytext.text = "Привет SoR!"; } 

Here is a small video review on the topic: http://www.youtube.com/watch?v=EOX6itCuKOc

  • >> You no longer need to work with the interface in onGUI, since the event system has been introduced. @AnatolyNikolaev I don’t agree that there’s no need, drawing on OnGUI is quicker and more flexible, moreover, if you start writing on the UI, you cannot mix homemade drawing and UI, as the homemade drawing is drawn with the last layer, and the UI can’t be pulled out bring to Front. - test123
  • The rest is a matter of habit, IMHO, it’s more convenient to add the Editor yourself and draw in WYSIWYG mode so that designers can see this case, but to save some resources (homemade code doesn’t always have anchors, scaling and a number of unused drawing properties), rather than making a UI and trying to build crutches in "difficult places": D - test123