Rename gameobject (game not running, via inspector script)

Is this possible?

  • create GameObject in Hierarchy
    Window
  • drag script onto GameObject that has a field called “name”
  • enter value in “name” field
  • name of GameObject in Hierarcy Window changes to match “name” field.

I know how to change a GameObject name while a game is playing. I know how to change a GameObject name via editor script, but I can’t attach an editor script to a GameObject. I suspect there is a silly, easy fix… Anyone know what it is?

Are you familiar with [ExecuteInEditMode] ?

Here’s what I came up with, from the top of my head (while I actually try something else out)

[ExecuteInEditMode]
public class MyClass : MonoBehaviour
{
    public string objectName;
    void Start()
    {
        objectName = gameObject.name;
    }

    void OnValidate()
    {
        gameObject.name = objectName;
    }
}

EDIT -
Well, Turns out : OnValidate() actually does what you want. And you don’t even need the ExecuteInEditMode tag.
So, just leave it out

Hope that helped!

It’s actually very simple to change a GameObject’s name. I believe you’re trying harder than you are supposed to. Go to your Hierarchy window, and select the object you want to rename by clicking on it. It will turn blue. Click it again and you will be able to change the name of it, no script required. Be careful though, do not double-click the object(meaning do not click the object twice too fast). This will do something completely different.