You can use OnMouseDown to detect when the object is clicked, and show the info in a GUIText or GUI.Label, like this:
var information: String; // define the text here
private var guiOn = false;
private var rect: Rect;
function OnMouseDown(){
guiOn = true; // enable gui and define position at point clicked
rect = Rect(Input.mousePosition.x, Input.mousePosition.y, 300, 100);
yield WaitForSeconds(5);
guiOn = false;
}
function OnGUI(){
if (guiOn){
GUI.Label(rect, information);
}
}
NOTES:
1- Attach this script to each object.
2- OnMouseDown doesn’t work in the iPhone.
3- All objects must have a collider or be a GUI element.
EDITED:
If you already have some common script attached to all blocks, you can just edit this script and add the code above - code changes made to a script affect all instances (except changes in initial values of public variables - these are kept by the Inspector).
Anyway, if you want to have only one script (maybe attached to the camera) you can do a raycast and find the object “under” the mouse pointer:
function Update(){
if (Input.GetMouseButtonDown(0)){
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
if (Physics.Raycast(ray, hit) && hit.transform.tag=="Block"){
var clickedObj = hit.transform;
// do whatever you want with clickedObj
}
}
}
The problem here is: how to know which information show for each block? To do that, you should have the information stored in each block, or some kind of id number to use as an index and retrieve the information from an array. Both have the same problem: you must attach a script to each block, and write the information or id number for each one in the Inspector.
Finally: you can use the object name or tag to store information - but I don’t know which are the limitations (name size, tag size, how much tags you can register etc.).
EDITED 2: Since you need only one territory name showing at one time, you should use a single separated script to show the name (attach it to the camera or any other object), and set variables in it when some territory is clicked. This also helps to set the message Box characteristics in the Inspector with the variable style. That’s the separated script - let’s call it ShowName.js:
static var information: String; // the territory defines this string...
static var rect: Rect; // the message position...
static var endTime: float = 0; // and the time it will appear
var style: GUIStyle; // you can set color, background etc. at the Inspector
function OnGUI(){
if (Time.time <= endTime){ // show message if there's a new end time...
GUI.Box(rect, information, style); // Box may be better to show the message
}
}
The territory script reduced a little, since the OnGUI part was moved to the script above. It now only needs to define the message, the rect occupied and the time to turn the message off:
var information: String; // define the text here, as before
function OnMouseDown(){
ShowName.information = information; // define the name and the position (below)
ShowName.rect = Rect(Input.mousePosition.x, Input.mousePosition.y, 300, 100);
ShowName.endTime = Time.time + 5; // name will appear for 5 seconds
}
EDITED 3: Now there’s the version with a static right aligned box and with variable height:
static var information: String; // the territory defines this string...
static var endTime: float = 0; // and the time it will appear
var style: GUIStyle; // you can set color, background etc. at the Inspector
var w: float = 300; // box width
function OnGUI(){
if (Time.time <= endTime){ // show message if there's a new end time...
var h = style.CalcHeight(GUIContent(information), w); // find the necessary height
GUI.Box(Rect(Screen.width-w,10,w,h), information, style);
}
}
The territory script, now more reduced yet:
var information: String; // define the text here, as before
function OnMouseDown(){
ShowName.information = information; // define the name
ShowName.endTime = Time.time + 5; // name will appear for 5 seconds
}