how to create a textbox upon clicking an object?

i got a script here but i cannot use it. There is no text being shown out. What i want is, when i click on an object, a textbox will appear telling me what the object is. Can anyone give me a hand ?

var infoText : GUIText;
private var infoMark : boolean = true;

function OnMouseDown () {
/*
if (pickupType == PickupType.FuelCell) {
var answer : boolean =false;
var playerThirdPerson = GameObject.Find(“Player”);
playerThirdPerson.GetComponent(ThirdPersonController).walkSpeed = 0;
playerThirdPerson.GetComponent(ThirdPersonController).trotSpeed = 0;
*/

var playerFP = GameObject.Find(“First Person Controller”);
playerFP.GetComponent(FPSWalker).speed = 0;

if(gameObject.name == “Photo Frame 2”) {
infoText.guiText.text = " Air-Well in a Shophouse: " +
"\n Shop houses are small built and there are only several openings to let the air flow for ventilation. " +
"\n Hence, Air-well was built for the purpose of natural lighting and ventilation. The occupant of the " +
"\n building also uses the Air-well to dry their laundry and collect rain water. The Air-well is effective " +
"\n yet simply built. An Air-Well is considered to be one the most prominent icon of the Shop house. " +
"\n \n When you finish your reading, enter ‘R’ to remove the question mark and proceed … " ;
yield new WaitForSeconds (3);
}
else if(gameObject.name == “Photo Frame 1”) {
infoText.guiText.text = " Ironing area n Air Well: " +
"\n Sewing machine wheel… " +
"\n Sewing machine wheel… " +
"\n Sewing machine wheel… " +
"\n Sewing machine wheel… " +
"\n \n When you finish your reading, enter ‘R’ to remove the question mark and proceed … " ;
yield new WaitForSeconds (3);
}

while (infoMark) {
if (Input.GetKey(“r”)) {
infoMark = false;
break;
}
else yield new WaitForSeconds (0.2);
}

playerFP.GetComponent(FPSWalker).speed = 6;
infoText.guiText.text = " " ;
Destroy(gameObject);

}

Do all the objects have Colliders? OnMouseDown only works for GUIElements or Colliders.

Is the problem with getting the clicks or displaying the text box? I would put in a few Debug.Log calls to see if you are getting the mouse clicks. You may also want to put in default text into infoText on Awake or Start (for testing) to make sure that it is being displayed without any mouse clicks.

Yes, the objects have colliders.

The problem is both, upon clicking the object, there aint any text box that appear.

I am trying something very similar to this,
and I keep getting a “You can only call GUI functions from inside OnGUI”
But since my guicode is triggered by a IF condition, it wont let me do it inside the ongui.

Could really use some help.

Here is my (much more simple code)

 private var highlightMultiply = 2.5;
private var originalColor;
var Glowing = 0;
var infoText : GUIText;

function Start () {
   // get original color for Glow functions
   originalColor = renderer.material.color;   
}

function OnMouseEnter(){
   Glow();
   Glowing = 1;
}

function OnMouseExit(){
   NoGlow();
   Glowing = 0;
}

function Glow() {
   renderer.material.color.r = originalColor.r*highlightMultiply;
   renderer.material.color.g = originalColor.g*highlightMultiply;
   renderer.material.color.b = originalColor.b*highlightMultiply;
}

function NoGlow() {
   renderer.material.color = originalColor;
}



if (Glowing == 1  Input.GetButton ("Use-look")) {
infoText.guiText.text = "A small wooden box";
}
}

Have a boolean in OnGUI that says whether the OnGUI code should run or not. e.g.,

private var showBox = false;

function Update () {
	if (Input.GetKeyDown("x")) showBox = true;
}

function OnGUI () {
	if (showBox) GUILayout.Box("X");
}

–Eric

YAY!:slight_smile:

Thanks for your help… Got it working^^