- Home /
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?
Answer by jokim · Aug 21, 2014 at 03:21 PM
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!
The other option I was going to try was to go with Editor scripts to expose properties. $$anonymous$$ake the name a property, showing in the inspector. Once the value change, set the gameobject.name as well.
Answer by JakeMcDig · Aug 20, 2014 at 11:35 PM
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.
$$anonymous$$y 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.
Your answer
Follow this Question
Related Questions
How do I assign script as a variable in the inspector? 1 Answer
custom inspector and edit mode script 1 Answer
one shot Sound on collision. Unity 4.0 problem. 2 Answers
Unity Crashes when C# script with code inside class but outside function is added to a GameObject 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers