Class member shows as None inside inspector

Hi,

I have a class (Test) that includes an enumeration to be used as a mask.

using UnityEngine;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System;

// Need Serializable and class attributes to make InitParms visible in the editor.
[Serializable]
public class Test : MonoBehaviour
{
  public enum Mask
  {
    one = 1,
    two = 2,
    four = 4,
    eight = 8,
  };

  public Test()
  {
  }

  public Mask m_Mask = 0;
}

In order to allow multiple items to be selected within variables of that enumeration’s type, I made a custom editor.

using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;

[CustomEditor(typeof(Test))]
public class TestEditor : Editor
{
  public override void OnInspectorGUI()
  {
    Test script = (Test)target;

    script.m_Mask = ((Test.Mask)EditorGUILayout.EnumMaskField("Mask", (Test.Mask)script.m_Mask)); 
  }
}

This worked fine until I tried to use that class as a separate class member inside a second class, TestClass.

using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;

public class TestClass : MonoBehaviour
{
  TestClass()
  {
  }

  public Test m_Test;
}

After adding both to scripts to a game object, I have problems viewing the TestClass script’s m_Test member variable. All that is displayed in the inspector is None(Test)

8979-testclass.jpg

If I delete the editor script, then the TestClass member variable m_Test displays properly, however I can no longer choose multiple items from the enumerated mask.

Any idea as to how to solve this problem? My ultimate desire is to add a member variable to a class and select multiple mask bits via the inspector. The attached image is to simply illustrate that the custom editor works so long as the Test.cs script is dropped directly onto the game object.

Thanks,
Brian

In your code, m_Test is simply a reference to a component… and it is null because you do not set it to anything. The inspector is providing a way for you to set the member variable interactively (by dragging any GameObject that has that component in).

When you say:

public Test m_Test;

You did not create an instance of the Test component. You created a variable that can reference one. Since you didn’t set it to anything, it’s null.

But setting it to something is not going to help because what you want to do takes a different approach. I think you are trying to have multiple Masks.

But even there you have issues. For example m_Mask is a Mask (an enum) not a bitfield. You probably want that to be an int, not an enum. You can use the enum potentially to help set the bits (as you are doing).

So you don’t want a special class to warp the enum here, certainly not a component.

Just create an int mask for each thing you want and a custom editor for that class that uses the EnuMMaskField() for each.