Click on object and display it's name

Hello, I’m completely new to Unity, and I’m currently trying to get this going.
A 3D-model which can be rotated (this seen from any angle), the model contains many objects. When you click on a particular object it’s name should be displayed.
I’ve a Mousedown script working (when you click the mouse (any were in the scene) it displays some text in the debug. Now I want the message only to display when I click on the object and to display that text somewhere at a fixed position onscreen.

Can somebody help me?
BTW I’m I doing good with the Mousedown or should I make the objects some type of button first?

Your doing good with the mousedown, but you need a mouse down on each object that you want to click on. Then reference it back to the gui on the screen.

// code attached to your camera
// name this script "myGUI"

var txt = "";

function OnGUI () {
GUI.Label (Rect (10, 10, 100, 20), txt);
}

// code attached to all of your objects

function OnMouseDown () {
Camera.main.gameObject.GetComponent("myGUI").txt = transform.name;
}

Thank you BigMisterB, it works like a charm!
Now I’m off to get it updating a list (highlighting the clicked on object name in a list). I’m trying to get it to do by myself or otherwise you’ll see a new thread on this great forum pretty soon;-)

Thank you again!
David

This script worked great up until now. I’m working with iOS now and when I’m trying to build I get the following error:
BCE0019: ‘txt’ is not a member of ‘UnityEngine.Component’.

I’ve search the forum and it has something to do with building for iOS (I think) and that the script has to be rephrased.
I’ve added “#pragma strict” already so I get the error already when saving the script instead only when building.
Anybody any ideas?

Edit 1: I’ve changed it into:
var txt : String = “”;
Since I read somewhere this requires strict annotation so I explicitly stated that txt is a string, but unfortunately it didn’t solve the problem. SO any help is appreciated

Edit 2 I’ve changed
Camera.main.gameObject.GetComponent(“myGUI”).txt = transform.name;
into:
(Camera.main.gameObject.GetComponent(myGUI) as myGUI).txt = transform.name;
which solved the error and the script works fine again. This seems the exact “strict” typing which was required.

hii my object name is doggi … an error is coming

Camera.main.gameObject.GetComponent(“myGUI”).txt = transform.doggi;

but error says - Assets/myGUI.js(13,62): BCE0019: ‘doggi’ is not a member of ‘UnityEngine.Transform’.

plz help

I think you might have accidentally replied to this thread when you meant to post a new one. (You can post a new thread using the ‘Post New Thread’ button at the top of each forum page.)

I’m quite sure he is using the code given above and replaced “name” in the script with the name of his object - which obviously doesn’t work.

So…
@ersaurabh101: it has to be “transform.name”. The name of your object doesn’t matter.