Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by AntFitch · Aug 20, 2014 at 09:57 PM · gameobjectinspectorrename

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jokim · Aug 21, 2014 at 03:44 PM 0
Share

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.

avatar image Maloke · Apr 23, 2017 at 12:51 AM 0
Share

Worked perfect for me in 5.5!!

avatar image
-1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AntFitch · Aug 21, 2014 at 03:05 PM 0
Share

$$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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges