x


Get GameObject name from other object.

Hey! I have a GameObject with a GUI script attatched to it, and i need to print the selected objects name (Useing raycast to select object), so i need to access the selected GameObject's name from the GUI script. Is this possible?

Thanks! //Tommy

more ▼

asked Oct 26 '10 at 08:16 PM

Tommy gravatar image

Tommy
156 20 22 27

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You can just use name, e.g. :

Debug.Log(name);

will print the name of the gameobject the script is attached to

You can use that like this to find out the name of the object under your cursor in c#:

void Update()
{
    RaycastHit hit;
    if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
    {
        Debug.Log(hit.collider.name);
    }
}

or js:

function Update()
{
    var hit : RaycastHit;
    if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit))
    {
        Debug.Log(hit.collider.name);
    }
}
more ▼

answered Oct 26 '10 at 08:33 PM

Mike 3 gravatar image

Mike 3
30.6k 10 67 254

(comments are locked)
10|3000 characters needed characters left

Try this, haven't tested it myself and not sure if a string would be returned but it's worth a try.

var itemHit : String;

function Update() {
  if (Physics.Raycast (...)) {
    itemHit = hit.collider;
  }
}
more ▼

answered Oct 26 '10 at 08:29 PM

Will 7 gravatar image

Will 7
109 6 8 15

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3419
x2170
x155
x148
x46

asked: Oct 26 '10 at 08:16 PM

Seen: 2529 times

Last Updated: Oct 26 '10 at 08:16 PM