- Home /
Questions exists : https://answers.unity.com/questions/1005277/can-i-change-variable-name-on-inspector.html
Change a variable name only on the Inspector
Can I change a variable name so that it shows its original name inside my script but on the Inpector some other name from my preference?
For example, I wanna change this variable name so that is shows some other name on the Inspector
Answer by Hellium · Apr 01, 2018 at 10:21 AM
For sure you can! And it's not that hard!
Create a first script called RenameAttribute.cs
and copy paste the following code:
using UnityEngine;
public class RenameAttribute : PropertyAttribute
{
public string NewName { get ; private set; }
public RenameAttribute( string name )
{
NewName = name ;
}
}
Then, create an other script called RenameDrawer
. Place this script in a folder called Editor
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(RenameAttribute))]
public class RenameEditor : PropertyDrawer
{
public override void OnGUI( Rect position, SerializedProperty property, GUIContent label )
{
EditorGUI.PropertyField(position, property, new GUIContent( (attribute as RenameAttribute).NewName ));
}
}
Finally, in your original script, add the attribute we've just created:
[Rename("My super properties!"]
public TPC_Properties properties;
It doesn't recognize the function Rename :/
All my scripts are inside the namespace SA, and I already tried taking all of them out of this namespace and putting the scripts you shared with me inside of this namespace as well and none worked.
I did put the scripts inside of a folder called Editor
, so I don't really know what else I could do. I'm using 2 scripts to draw the components of a scriptable object on the Inspector and they work just fine.
Any thoughts?
The name of the 2nd script should be
RenameEditor
ins$$anonymous$$d of`RenameDrawer` (or change the name of the class), my bad, sorryThe
RenameAttribute.cs
file, should be outside of theEditor
folder
The namespaces should not be a problem. But the two classes in your namespace if it's more convenient for you.
Is there any way to make this work with Array ? Right now it seems to be rena$$anonymous$$g each element of the array but not the array itself. Thanks
Answer by RVDL_IT · Apr 01, 2018 at 12:45 AM
No, you can't. If you really want to you can make the variable private and make a new variable that is the same.
public bool PublicBool;
private bool OriginalBool;
PublicBool = OriginalBool;
Follow this Question
Related Questions
How can i clear specific printed text to console window from code ? 0 Answers
Checking a list containing another list, and checking if the elements in the list are equal problem 1 Answer
how to rotate a Rigidbody2D player on a moving platform. 0 Answers
help with instantiating gameobject at random postition from a target gameobject? 1 Answer