Forum rules - please read before posting.

Creating a popup list in a custom Action from an array

Hi,

I have a string array in a singleton pattern script, which I would like to be shown as a pop up in a custom Action. I tried following various tutorials on the EditorGUILayout.Popup to achieve this, but I'm not sure if this is possible with Adventure Creator, or I'm just not doing things correctly.

As mentioned above, I have a script that uses singleton pattern:

public static TasksManager instance;

    public static int MAX_TASKS = 100;
    public string[] task = new string[MAX_TASKS];
    public int taskIndex;

    // Makes it a Singleton
    void Awake()
    {
        if (instance != null)
        {
            Destroy(gameObject);
        }
        else
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
            InitTasks();
        }
    }


    //Set up tasks
    void InitTasks()
    {
        int i = 0;

        task[i] = "This is your first task";
        i++;
        task[i] = "Second task";
        i++;
    }

And later, in a custom action under override public void ShowGUI ():

        TasksManager.instance.taskIndex = EditorGUILayout.Popup(TasksManager.instance.taskIndex, TasksManager.instance.task);

But the Action is not displaying anything (just a grey box) and I keep getting an error "NullReferenceException: Object reference not set to an instance of an object" on the line under GUI.

Again, I have no idea if this is AC related problem, or a Unity one.

Comments

  • This'll be code-related, not AC. You're assigning your "instance" variable in Awake, which means it'll be null while in Edit mode. Better to make "instance" private and make a public getter.

    Something along the lines of:

    public static TasksManager Instance
    {
        get
        {
            if (instance == null)
            {
                instance = (TasksManager) Object.FindObjectOfType <TasksManager>();
                instance.InitTasks();
            }
            return instance;
        }
    }
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.