I’m trying to automate the process of drawing node GUI. Here are my (desired) steps.
I want to get a list of variables from a class.
I want to loop through each variable and get its type. (is it an int, float, or something else?)
Based on the variable’s type, I want to call a GUI function to display and set the variable’s value.
(If it’s an int, I’d call the IntField method and pass in the variable’s value while setting its value to whatever the GUI function returns)
Here’s my current code. I have no idea what is triggering the (Null Reference) errors. Each frame, the chance of getting an error is hit or miss. If I pause the game and play it frame by frame, I don’t always get a new error.
Here’s my code:
/// <summary>
/// Draws a given field.
/// </summary>
/// <param name="field"></param>
static void DrawNodeField (NodeBase node, FieldInfo fieldInfo)
{
Type fieldType = fieldInfo.FieldType;
object fieldValue = fieldInfo.GetValue(node);
if (fieldValue != null && fieldInfo != null && fieldType != null)
{
if (fieldValue is int)
// The error points the the line below this comment
fieldInfo.SetValue((int)fieldValue, EditorGUILayout.IntField(fieldInfo.Name, (int)fieldValue));
else if (fieldValue is float)
fieldInfo.SetValue((int)fieldValue, EditorGUILayout.FloatField(fieldInfo.Name, (float)fieldValue));
else if (fieldValue is PlacementType)
fieldInfo.SetValue((int)fieldValue, EditorGUILayout.EnumPopup(fieldInfo.Name, (PlacementType)fieldValue));
}
else
Debug.Log("FieldValue of " + fieldInfo.Name + " is null.");
}
And here’s the error:
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.GUILayoutUtility.DoGetRect (Single minWidth, Single maxWidth, Single minHeight, Single maxHeight, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[ ] options) (at C:/buildslave/unity/build/Runtime/IMGUI/Managed/GUILayoutUtility.cs:374)
UnityEngine.GUILayoutUtility.GetRect (Single minWidth, Single maxWidth, Single minHeight, Single maxHeight, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[ ] options) (at C:/buildslave/unity/build/Runtime/IMGUI/Managed/GUILayoutUtility.cs:371)
UnityEditor.EditorGUILayout.GetControlRect (Boolean hasLabel, Single height, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[ ] options) (at C:/buildslave/unity/build/Editor/Mono/EditorGUI.cs:6857)
UnityEditor.EditorGUILayout.IntField (System.String label, Int32 value, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[ ] options) (at C:/buildslave/unity/build/Editor/Mono/EditorGUI.cs:5548)
UnityEditor.EditorGUILayout.IntField (System.String label, Int32 value, UnityEngine.GUILayoutOption[ ] options) (at C:/buildslave/unity/build/Editor/Mono/EditorGUI.cs:5542)
Vexus.NodeUIUtilities.DrawNodeField (Vexus.NodeBase node, System.Reflection.FieldInfo fieldInfo) (at Assets/Vexus 2/Scripts/Data/Utilities/NodeUIUtilities.cs:61)
Vexus.NodeUIUtilities.DrawNodeUI (Vexus.NodeBase node) (at Assets/Vexus 2/Scripts/Data/Utilities/NodeUIUtilities.cs:20)
Vexus.NodeGroup.UpdateNodeGroup () (at Assets/Vexus 2/Scripts/Data/NodeGroup.cs:69)
VexusFlowTest.Update () (at Assets/Vexus 2/Scripts/Runtime/VexusFlowTest.cs:26)