Get an object's name

As always apologies if this is a repost, but I’ve search the forums and the script reference and come up empty.

What I’d like to do is make a GameObject aware of its name as assigned to it in the scene editor. Is this possible?

if(GUILayout.Button("Click me"))
{
	Debug.Log(this.gameObject.name);
}

put this in an OnGUI() function on the gameObject

1 Like

Get used to using the scripting class list. This link shows what you get when you search for “name”. It shows you what you need and oh so much more.

Hot, thanks folks.

Vimalakirti: yeah, I did a search for name, but it doesn’t list the GameObject.name field. Apparently, because it’s not a field of GameObject, which is I guess what was confusing me. Rather it appears to be inherited from Object, which is strange because I was under the impression Object was defined in mono, not Unity.

System.Object is defined in Mono (actually, the .NET API); UnityEngine.Object is defined by Unity :-/ Which you get depends on what ‘using’ statements you have and/or the order they’re in…

Oh wonderful.

There’s a System.Object which is the same as object, and Object from UnityEngine. They shouldn’t interfere, and most intelligent autocomplete won’t autocomplete the wrong thing :slight_smile:

Well sure, but just because you can get something to work doesn’t mean it’s well designed.