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

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);
    }
}

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;
  }
}