I have the following custom class:
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
public Vector3 var1 = new Vector3(1,2,3);
public string var2 = "ghhghahgh";
}
Said class has a properly working custom editor:
using UnityEngine;
using UnityEditor;
[CustomEditor (typeof(test))]
public class testeditor : Editor {
test varelement;
public override void OnInspectorGUI() {
varelement = (test)target;
varelement.var1 = EditorGUILayout.Vector3Field("Var 1", varelement.var1);
varelement.var2 = EditorGUILayout.TextField("Var 2", varelement.var2);
if(GUI.changed) {
EditorUtility.SetDirty(target);
}
}
}
Now I need to include a ‘test’ instance in another script:
using UnityEngine;
public class bigclass: MonoBehaviour {
public test mytest;
//several other components
}
THE RETARDED PROBLEM
No matter what I do, I can’t get a custom editor to work for ‘bigclass’ since I receive
two OnInspectorGUI nullreferenceexception
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(bigclass))]
public class bigclasseditor : Editor {
public override void OnInspectorGUI()
{
bigclass myclass = (bigclass)target;
//MYTEST IS -ALWAYS NULL- REGARDLESS OF HOW WELL IT WORKS AS A SINGLE COMPONENT
myclass.mytest.var1 = EditorGUILayout.Vector3Field("Var 1", myclass.mytest.var1);
//put here to understand how the inspector sees the component
DrawDefaultInspector();
}
}
As a matter of fact, the inspector shows it as an object, with ‘none’ value without the custom editor, so why on earth it’s not initialized to its default values as it does when it’s instantiated singularly?
I’ve even tried converting to stand alone classes, but doing such I can’t build a custom inspector for them since I am unable to assign the ‘target’ to ‘test’ for iteration.
"CS0030: Cannot convert type `UnityEngine.Object' to `test'"
There must be something I’m missing to see here, but despite hours of searching and trying, I can’t understand why the ‘test’ instance isn’t instantiated by ‘bigclass’. It makes no sense at all. I even tried the executeineditormode approach without the slightest change in result.
Hopefully there’s someone able to help with this issue. Even refactoring the whole data structure led me to the same problem.