I want to make GUI Text to show up when I hover mouse over gameObject, and I want to display gameObject’s name. Problem is that when I hover over gameObject GUI Text is created but I cant seen it anywhere. Here is part of code that uses GUI Text:
var name : String = "Player"
var textObject : GUIText;
function OnMouseEnter() {
textObject.text = gameObject.name;
Instantiate(textObject, transform.position, transform.rotation);
}
The position item of GUIText is determined by coordinates on the screen, instead of by coordinates in the three-dimensional world. So, if you execute function when a mouse to be over object, it is necessary to transfer object coordinates to coordinates on the screen. See below:
var name : String = "Player"
var textObject : GUIText;
function OnMouseEnter() {
textObject.text = gameObject.name;
//I precisely am not sure, whether Java will be able correctly to transfer one type to another without additional records
var myT:GUIText = Instantiate(textObject, Vector3.zero, Quaternion.identity); //default for Guitext position and rotation
var posScr:Vector3 = Camera.main.WorldToScreenPoint (this.transform.position)
myT.pixelOffset = Vector2(posScr.x, posScr.y);
}
You will see a text at center of your object. It will be more exact where you located pivot point. To move a text up you need to transform other coordinates. For an everyday occurence, it is current position + a half of height of object on a y axis. And attach this script in your object.