Editor not showing default values
Hello. I have the following classes:
[System.Serializable]
class A
{
public float a = 50f;
public float b = 100f;
//rest of class...
}
[System.Serializable]
class B
{
public A[] myArray;
//rest of class...
}
class C : MonoBehaviour
{
public B myB;
//rest of class...
}
For some reason every time I create a new element for "myArray" in the editor, it gets created with a=0 and b=0, and I would want to have the default values 50f and 100f. Has anyone encountered this and know what causes it? I should also mention that I have a custom editor for class C that just adds some buttons like so:
[CanEditMultipleObjects]
[CustomEditor(typeof(C))]
public class C: Editor
{
public override void OnInspectorGUI()
{
if(GUILayout.Button("Do something"))
{
//....
}
base.OnInspectorGUI();
}
}
Thank you for your time :)
Answer by streeetwalker · Mar 29, 2020 at 07:48 AM
@n_rusev, you have constructors in each of your classes, and you are calling the constructors to create instances of them? And you are creating new instances of A and adding them to your array in class B?
No, as you can see class C extends $$anonymous$$onoBehaviour, which means I'm editing the public values from the editor.
But wait, when you declare, public A[] myArray, myArray does not get populated with anything automatically. You have to dimension the array first and then you have to instance the objects and assign them to the array elements.
Otherwise, how do you imagine it working? How many array elements do want to display of class A's default values? For argument, let's say, 10. Without actual instances of class A, when you alter the 9th element default values, what of A gets those values? Without instances of A, and B, what you are trying to do won't make sense.
@n_rusev, I have to clarify: it is a bit deceiving what Unity does with an inspector.
When you declare a public array of simple variable type, float, or struct type Vector3, for example, and dimension it using the inspector, it will create an array populated with those types using the values you entered in for each element when you run the game.
However it does not do the same for objects. Declaring an array of GameObject, or any other type of object that needs to be instantiated, does not create an array of those actual objects - only an array of empty place holders of that type.
I think the only way around your problem is to have script C [ExecuteInEdit$$anonymous$$ode] and get the dimension you've entered into the inspector, and then populate the array with actual instances of A. That may be a trick to do, and have it stick when the game runs.
Thank you for your replies. Your second reply was very helpful. I see now why I get this problem - as you said unity creates only empty place holders for the future objects. I forgot to mention that I only needed the first element to have the default values, since every other copies the previous and that is fine for what I'm trying to do and in my case actually it won't make sense to use this script with empty array in class B, so just changing the code from public A[] myArray; to public A[] myArray = new A[1]; populated the fields with the default values.
Answer by Lyje · Feb 12 at 06:30 PM
Assuming the containing class is a MonoBehaviour or ScriptableObject, there's a workaround for this to automatically replace editor-created instances with correctly constructed instances.
[System.Serializable]
public class MyPlainClass {
public float a = 100;
public bool isInitialized = true;
}
public class MyContainerClass : MonoBehavior, ISerializationCallbackReceiver {
public MyPlainClass[] myArray;
public void OnBeforeSerialize(){
for (int i=0; i<myArray.Length; ++i){
if (!myArray[i].isInitialized) myArray[i] = new MyPlainClass();
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612230008im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is it possible to customize how a variable of a certain type would look in the inspector? 1 Answer
Generate object diagram from unity editor 0 Answers
How do you fix slow Unity Editor run on Windows 10? 1 Answer
How to make -show in explorer- work properly (from dropdown menu in Project tab) 1 Answer