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?
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!
The other option I was going to try was to go with Editor scripts to expose properties. Make the name a property, showing in the inspector. Once the value change, set the gameobject.name as well.
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.
My title wasn't very clear, so I've changed it. I specifically need to change the GameObject name to a string in a script. Editor scripts are the only ones that I know of that let you change this type of data while the game is not running. The problem? I can't attach an editor script to a GameObject and for the tool I'm creating, the script must be attached to the GameObject.
The other option I was going to try was to go with Editor scripts to expose properties. Make the name a property, showing in the inspector. Once the value change, set the gameobject.name as well.
– jokimWorked perfect for me in 5.5!!
– Maloke