Forum rules - please read before posting.

Remember Script Enable/Disable

Hello, I have a script attached to a gameObject and I want to use Adventure Creator to remember whether the script is enabled or disabled. Could you please guide me on how to achieve this? I did come across some "Remember" scripts, but I couldn't find any that would serve this purpose.

Comments

  • A GameObject's enabled state cannot be stored on the same object - because the Remember component itself must be enabled at the time of saving.

    What you instead need to do is have a separate GameObject that records the state(s).

    RememberMultipleEnabled.cs:

    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 () { }
    
    }
    

    And the Editor script, RememberMultipleEnabledEditor.cs:

    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 ();
        }
    
    }
    
  • Sorry, I'm, trying to understand how this works:

    So I saved the RememberMultipleEnabled in my custom script folder and the RememberMultipleEnabledEditor in the AC\Scripts\Save System\Editor folder.
    I created a gameObject in the scene named SaveControl and I attach the RememberMultipleEnabledscript to it. Is that it?

    Just to clarify, I need to enable/disable the component of the gameObject, not the gameObject itself, in case I expressed myself incorrectly.

  • Ah. In that case, it's simpler.

    You can either control the enabled state of your components with an Animator, and then use Remember Animator, or use this variant of the above that saves a component's enabled state:

    using UnityEngine;
    using AC;
    
    public class RememberComponentEnabled : Remember
    {
    
        public MonoBehaviour componentToSave;
    
        public override string SaveData ()
        {
            ComponentEnabledData data = new ComponentEnabledData();
            data.objectID = constantID;
            data.savePrevented = savePrevented;
    
            data.isEnabled = componentToSave.enabled;
    
            return Serializer.SaveScriptData <ComponentEnabledData> (data);
        }
    
        public override void LoadData (string stringData)
        {
            ComponentEnabledData data = Serializer.LoadScriptData <ComponentEnabledData> (stringData);
            if (data == null) return;
            SavePrevented = data.savePrevented; if (savePrevented) return;
    
            componentToSave.enabled = data.isEnabled;
        }
    
    }
    
    
    [System.Serializable]
    public class ComponentEnabledData : RememberData
    {
    
        public bool isEnabled;
    
        public ComponentEnabledData () { }
    
    }
    

    and:

    using UnityEngine;
    using UnityEditor;
    using AC;
    
    [CustomEditor (typeof (RememberComponentEnabled), true)]
    public class RememberComponentEnabledEditor : ConstantIDEditor
    {
    
        public override void OnInspectorGUI ()
        {
            RememberComponentEnabled _target = (RememberComponentEnabled) target;
            _target.componentToSave = (MonoBehaviour) EditorGUILayout.ObjectField ("Component:", _target.componentToSave, typeof (MonoBehaviour), true);
            SharedGUI ();
        }
    
    }
    
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.