Forum rules - please read before posting.

Getting an error on "Remember Multiple Enabled" script

Hey community!

I have the following script which I use to keep track of objects that are activated and deactivated in scene:

using UnityEngine;
using AC;

public class RememberMultipleEnabled : Remember
{

    public GameObject[] objectsToSave;

    public override string SaveData()
    {
        EnabledMultipleData data = new EnabledMultipleData();
        data.objectID = constantID;
        data.savePrevented = savePrevented;

        data.isEnabled = new bool[objectsToSave.Length];
        for (int i = 0; i < objectsToSave.Length; i++)
        {
            data.isEnabled[i] = objectsToSave[i].activeSelf;
        }

        return Serializer.SaveScriptData<EnabledMultipleData>(data);
    }

    public override void LoadData(string stringData)
    {
        EnabledMultipleData data = Serializer.LoadScriptData<EnabledMultipleData>(stringData);
        if (data == null) return;
        SavePrevented = data.savePrevented; if (savePrevented) return;

        for (int i = 0; i < data.isEnabled.Length; i++)
        {
            objectsToSave[i].SetActive(data.isEnabled[i]);
        }
    }

}


[System.Serializable]
public class EnabledMultipleData : RememberData
{

    public bool[] isEnabled;

    public EnabledMultipleData() { }

}

I'm getting a Null Refererence Exception Error on line 12, but I don't have an idea what's wrong here. The script works as intended when I open another scene and then open the original one again. How can I fix this?

Comments

  • I don't get an error when saving with it in the scene on my end - what are your AC/Unity versions, and what is the exact error message in full?

  • Here's the error:

    NullReferenceException: Object reference not set to an instance of an object
    RememberMultipleEnabledEditor.OnInspectorGUI () (at Assets/AdventureCreator/Scripts/Save system/Editor/RememberMutlipleEnabledEditor.cs:12)
    UnityEditor.UIElements.InspectorElement+<>c__DisplayClass59_0.b__0 () (at :0)
    UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

    Unity version: 2020.3.34f1
    AC version: 1.77.3

  • The error isn't from this script - its from your Editor script.

  • You're absolutely right, sorry. Here's the correct script. There was a typo in the filename, I renamed the script but I'm still getting the same error in the editor script. What could be the cause?

    using UnityEngine;
    using AC;
    
    public class RememberMultipleEnabled : Remember
    {
    
        public GameObject[] objectsToSave;
    
        public override string SaveData()
        {
            EnabledMultipleData data = new EnabledMultipleData();
            data.objectID = constantID;
            data.savePrevented = savePrevented;
    
            data.isEnabled = new bool[objectsToSave.Length];
            for (int i = 0; i < objectsToSave.Length; i++)
            {
                data.isEnabled[i] = objectsToSave[i].activeSelf;
            }
    
            return Serializer.SaveScriptData<EnabledMultipleData>(data);
        }
    
        public override void LoadData(string stringData)
        {
            EnabledMultipleData data = Serializer.LoadScriptData<EnabledMultipleData>(stringData);
            if (data == null) return;
            SavePrevented = data.savePrevented; if (savePrevented) return;
    
            for (int i = 0; i < data.isEnabled.Length; i++)
            {
                objectsToSave[i].SetActive(data.isEnabled[i]);
            }
        }
    
    }
    
    
    [System.Serializable]
    public class EnabledMultipleData : RememberData
    {
    
        public bool[] isEnabled;
    
        public EnabledMultipleData() { }
    
    }
    
  • This is the same as the above script.

  • For Pete's sake... Sorry, man. Here's the correct script:

    using UnityEngine;
    using UnityEditor;
    using AC;
    
    [CustomEditor(typeof(RememberMultipleEnabled), true)]
    public class RememberMultipleEnabledEditor : ConstantIDEditor
    {
    
        public override void OnInspectorGUI()
        {
            RememberMultipleEnabled _target = (RememberMultipleEnabled)target;
            int numToSave = _target.objectsToSave.Length;
            numToSave = EditorGUILayout.DelayedIntField("# objects to save:", numToSave);
            if (numToSave != _target.objectsToSave.Length)
            {
                GameObject[] backup = _target.objectsToSave;
                _target.objectsToSave = new GameObject[numToSave];
                for (int i = 0; i < Mathf.Min(numToSave, backup.Length); i++)
                {
                    _target.objectsToSave[i] = backup[i];
                }
            }
    
            for (int i = 0; i < _target.objectsToSave.Length; i++)
            {
                _target.objectsToSave[i] = (GameObject)EditorGUILayout.ObjectField("Object #" + i + ":", _target.objectsToSave[i], typeof(GameObject), true);
            }
            SharedGUI();
        }
    
    }
    
  • No problem - thanks, I see the issue now.

    It's a case of the objectsToSay array not being declared - so it defaults to null. To fixed, replace:

    public GameObject[] objectsToSave;
    

    with:

    public GameObject[] objectsToSave = new GameObject[0];
    
  • Thanks, everything's working now just the way it should :)

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.