The question is answered, right answer was accepted
Why do Constructors return null when used within EditorWindow
I'm writing a Popup-ytype window to be used for creating "Effects" for character skills an Items and this Bizzare behaviours starts happening. Its one thing to have Unity inbuilt class constructors return null (Like Avatar) but a custom class that I wrote that uses virtually a default constructor?
public void AddEffect(int lastIndex)
{
effectIndex = lastIndex;
//proof that my variable isn't being reassigned
var effect = new Effect();
Debug.Log(effect); //return null everytime
//variable I'm trying to assign
this.currentEffect = new Effect(); //also null
The strange thing is the first line works. effect index get correctly assigned, but anytime i call a constructor it returns null. Also all Unity-Inbuilt classes/structs such as Font or Vector3 work. . Here is my Effect class
[System.Serializable]
public class Effect : UnityEngine.Object
{
public int code = 0; //Deal Damage
public int code2; //What formula
public string id; //ID
public int intValue; //Element
public int frame; //Apply effect during this frame of the animation
public float floatValue; //variance
public string stringValue;
public bool boolValue; //is crititcal
//Added for an insanity test, not actually needed
public Effect()
{
code = 0;
code2 = 1;
id = "id";
intValue = 12;
frame = 60;
floatValue = 3.1f;
stringValue = "hello";
boolValue = false;
}
}
}
and the AddEffect() is called from another Editor script called skill Editor, DrawBlankEffect() is called from OnGUI(), the idea is when the Effect editor is finished editing it fires a callback to skill editor that applies the changes in the Effects array.
/// <summary>
///
/// Fucntions as an "Add" button
/// </summary>
void DrawBlankEffect()
{
EditorGUILayout.BeginHorizontal();
GUILayoutOption[] myOptions = new GUILayoutOption[] { GUILayout.Width(60) };
GUIStyle boxstyle = EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector).box;
bool clicked = GUILayout.Button("", boxstyle, myOptions) || GUILayout.Button("", boxstyle, myOptions); ;
if (clicked)
{
Debug.Log("Blank Effect");
var window = EffectEditorWindow.Init();
window.AddEffect(2);
window.addEffectEvent += new EffectEditorWindow.AddEffectDel(AddNewEffect);
}
EditorGUILayout.EndHorizontal();
}
I didn't think a null returning constructor was actually thing that can be done, let alone have it done automatically to my code. All code examples are with their own namespace, the two editor scripts in a respective Editor Namespace. All using statements are present.
can some please explain what exactly is happening here? Why does only Effect reutrn null in its constructor? if you need more information just post what you need in the comments
thanks in Advance
Answer by Fornoreason1000 · Oct 02, 2016 at 08:01 AM
there is infact a Fault with the Effects scripts.
it inherits from UnityEngine.Object. As @Bunny83 around 9 months ago.
you should not derive your own types from UnityEngine.Object. That class has a special meaning to Unity and is always linked to a native C++ counterpart. If you create an instance of your own UnityEngine.Object derived type it will become a fake-null-object.
so thats what happens when you have a "fake null object".
Follow this Question
Related Questions
Save a custom Class Array 0 Answers
[Solved] Array is empty after Scene change.. 1 Answer
Help with classes and object constructors 1 Answer
NullReferenceException with serialized array 0 Answers
Very slow performance when variable has serialize tag 3 Answers