- Home /
Is it possible to make a C# script variable changeable in the Editor, without being public?
I have some C# scripts with a bunch of variables that I'd like to be able to change in the editor, to make testing and iterating easier, however I don't really want to have any of those variables be public since they really shouldn't be changed by anything within the game itself at runtime.
Though this is generic to a lot of scripts in my game, as an example in my Weapon script I have some variables like reload time, damage, firing speed etc... Now obviously nothing else should be able to ever change those variables, but I'd like to be able to initially set them from the editor so that I can control everything from within Unity itself rather than having to go out to various XML files or whatever to change things.
Is there any good way I can do this? My temporary solution was to have a public and private version of each variable. The public one gets set in the editor, and then on object creation the value is copied to the private version, and the public version is then ignored. That feels pretty awful to me though, doubling the amount of variables in each script and then never even using half of them after the first second.
Is there some proper way to do this, or should I just give in and load in values like this from XML files instead? That certainly feels cleaner, but I'd really like to have everything in the Editor itself.
Answer by Loius · Nov 08, 2013 at 03:19 PM
You can have a custom editor call in to a function in the class itself:
[Thing.cs]
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class Thing : MonoBehaviour {
[SerializeField] private int count;
#if UNITY_EDITOR
public void ShowEditGUI() {
count = EditorGUILayout.IntField("Count",count);
}
#endif
}
...
[ThingInspector.cs]
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Thing))]
public class ThingInspector : Editor {
public override void OnInspectorGUI() {
(target as Thing).ShowEditGUI();
}
}
That sounds perfect, thank you!
One thing I'm wondering though, since I don't see anything like a ListField even though the editor does support them, would a list require a for loop or something to show each entry in the list manually? Something like:
public void ShowEditGUI()
{
foreach (ListItem item in variableList)
{
item = EditorGUILayout.IntField("Item", item);
}
}
Is that sensible? Or is there something that does that sort of thing automatically that I'm missing?
As far as I know there isn't a simple thing that automatically does that. (you'd have to use a for loop though; foreach variables can't be altered)
Hmm, I've just tried implementing this and it seems that simply putting [SerializeField] in front of a List variable, without creating an Inspector class or making a ShowEditGUI function at all works automatically.
I guess that means the latter part is only necessary if you're using custom structs/classes within your class that Unity won't know how to display otherwise?
I haven't not made a custom editor for a class in forever!
You're right, just SerializeField is enough if there's no custom editor at all.
Your answer
Follow this Question
Related Questions
RTS Style Selection system (How to control one unit and not the others) 2 Answers
Help with making a script in the unity editor 2 Answers
Private variable losing its value on editor script 1 Answer
Variables not visible in editor but accessible from other scripts 2 Answers
Always play music? 1 Answer