- Home /
Question by
Tommy · Oct 26, 2010 at 08:16 PM ·
gameobjectaccessnameother
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
Comment
Answer by Mike 3 · Oct 26, 2010 at 08:33 PM
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);
}
}
Answer by Will 7 · Oct 26, 2010 at 08:29 PM
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; } }