Truly stupid question, I'm sure

First of all, I am a fairly experienced C++ programmer and I feel like I am quickly picking up the Javascript and Unity engine. I have even threw together a ‘Pong meets Air Hockey’ game in about a day.

BUT…

I am having the WORST time trying to get an integer, the player score, on screen! I have created a GUI Text object and attached it to this script:

var score1 = StartGame.player1Score;
var prefix = "";
var GUIScore1 : GUIText;	

function OnGUI(){
	GUI.Box (Rect(0,10,100,40), "Player 1 Score");
	GUIScore1.text = prefix + score1;
	GUI.Box (Rect(Screen.width - 100,10,100,40), "Player 2 Score");
	}

where GUIScore1 is attached to my GUIText object. The object is right in the center of the camera, but no matter what, nothing shows up! I’ve tried typing in ‘dummy text’, but nothing shows up in the object.

I’ve also tried converting integers to string, but I don’t know how to get the GUI object to display text in a label. It doesn’t seem to like a converted string in a GUI.Label function.

I know this is super basic but it’s really all I need to know for now.

Help??[/code]

GUIText and OnGUI don’t generally mix very well (OnGUI is always on top no matter what), so I wouldn’t recommend doing that. Use a label, such as “GUI.Label(Rect(whatever), score1.ToString());”.

–Eric

Working for me!

I made a empty game object named it GUI, and attached your script to it. Then I made a GUItext named it GUIScore1. Then I selected the GUIScore1 in the GUI inspector.

Check so u havent unchecked the GUILayer in the camera!

This worked!! BUT - now I’m a little confused. When I tried to move the GUItext object in the scene editor I could never get it to show up in the scene again. How do you move the text around the screen?

guitext / guitexture are true 3D object, they are thus moved the same way as all other 3D objects.

if they shall remain in front of the cam, ensure that you make them childs of the cam

That’s not even a little bit true. :wink: GUIText/Textures are not treated as 3D objects at all, they don’t have to be children of the camera, or anything like that. In fact that’s a very bad idea because that will cause the GUIText/Textures to move when the camera moves, and you do NOT want that. You need 1) a GUILayer component on the camera, and 2) GUIText/Textures are in viewport space, which means they must be always positioned in the range between 0 and 1 in order to be visible. (Except for the Z coordinate, which is used to layer them, and can be anything.)

–Eric

Hey if you need some help just PM me and I will help you.

What we do at MBO for GUI stuff is like this:

//batting box
GUILayout.BeginArea (Rect (Screen.width*adjustright1-battingbox.fixedWidth/2-adjustbox, Screen.height*adjustup1,Screen.width,Screen.height));
						GUILayout.Label (RD_texture,battingbox );
GUILayout.EndArea();

This is a texture but it could just as easily have been a text value like a score. This is with reference to Screen.width*adjustright1-battingbox.fixedWidth/2 so if I set adjustright1 to 0.5 in the inspector then i subtract have the width of the texture guess what ? Its in the middle of the screen.

Of course the code using GUILayout has to be in function OnGUI ().