Home > other >  Unity custom class constructor returns null
Unity custom class constructor returns null

Time:09-23

I have a custom class for I want to initialize in a custom editor. But, for some weird reason, the constructor returns null!

Edit: Yes, in Unity, this is possible.

Here is an Example: https://answers.unity.com/questions/1250722/why-do-constructors-return-null-when-used-within-e.html

Also, I am sorry if I am not using the correct terminology (a constructor does not return, thanks for pointing that out) but I still thought that the question was understandable.

Here is the Code where the exception is thrown:

[CustomEditor(typeof(ComponentMasterBase), true)]
public class ComponentMasterEditor : Editor
{
    public override void OnInspectorGUI()
    {
       ComponentMasterBase cmb = (ComponentMasterBase)target;

       //unrelated stuff here 


        if(GUILayout.Button("Add "   cmb.GetComponentType().DisplayName   " Slot"))
        {
            var c = new ComponentSlot(1, null, cmb.GetComponentType(), cmb);
            Debug.Log(c); //Output: null
            cmb.AttachComponentSlot(c); //Throws NullReferenceException
        }
    }
}

Here is the (slightly simplified) class:

public sealed class ComponentSlot : MonoBehaviour
{

    #region Size
    public static int MaximumSize = 6;
    private int _size = 1;
    public int Size
    {
        get { return _size; }
        set
        {
            int prevSize = _size;
            if (value > 0)
            {
                if (value <= MaximumSize)
                    _size = value;
                else
                    _size = MaximumSize;
            }
            else
                _size = 1;
            if(prevSize != _size)

                this.name = "Size "   _size   " "    ComponentType.DisplayName   " Slot";
            
        }

    }
    #endregion

    #region Component
    private ShipComponent component;
    public void SetComponent(ShipComponent c)
    {
        if (c == component) return;
        if (c == null) return;  //This should return out of the function, so no parenting stuff going on
        if(component != null)
            DestroyImmediate(component);
        component = c;
        c.transform.SetParent(this.transform);
    }
    public void UpdateComponent(){}
    public ShipComponent RetrieveComponent(){}
    public void DetachComponent(){}
    #endregion

    public ComponentTypes ComponentType;
    public ComponentMasterBase Owner;
    
                      // 1         null               eg ComponentTypes.Weapon  caller
    public ComponentSlot(int size, ShipComponent com, ComponentTypes t,         ComponentMasterBase master)
    {
        Size = size; SetComponent(com); ComponentType = t; Owner = master;
        Debug.Log("Created new weapon slot."); // This is printed.
    }
    public ComponentSlot() {} 
}

I do not know if the code I provided is enough or too much, because I have no clue where the issue lies here. I have never heard of a non nullable type constructor returning null :(

CodePudding user response:

When you using the mono behaviour class then you can't to call constructor because mono classes created early)))) Custom classes must to be without mono behaviour and without using function of that class.

  • Related