Forum rules - please read before posting.

Saving and Loading a disabled GameObject

Another weird scenario that no one else seems to have gotten into based on searches on this forum for an answer. I have a custom Remember script which is saving and loading whether particular bools are true or false. The script itself works fine however the GameObject that it is attached to (and the one that has the custom script with the relevant bools I'm saving/loading) isn't always active in the scene because it is in a menu (which when not on gets disabled by AC).

The menu in question is a tablet that the player checks through, I have a script that will show some email messages to the player and in that script it contains string data for what each email contains as well as bools which specify which emails are shown or not (so I simply change bools to true or false in game and it then either shows or hides a particular email). I understand that perhaps the correct way to do this would be to link each email to an element in the Menu Manager and then use AC to control how and when to view emails however my menu for the tablet is already very cluttered in the MenuManager with a ridiculous amount of elements and I am now confident enough with custom scripting to get the desired result via a custom script (and I just find it a lot easier to make changes to the required changes to the emails via the custom script).

So if I save the scene when the player isn't looking at their tablet/emails then the Email GameObject is disabled and so the remember script doesn't get called to make the save (and even if it was active to save the data I find that when the scene loads in the menu resets to being off so the Email object is disabled and so regardless the remember script doesn't get called to load the data anyway).

I can imagine questions like this can be quite infuriating, I mean 'Of course a script that is disabled won't get called you idiot!' Haha! However hopefully there is something simple I'm missing here that will help - I'm away from my project until tomorrow so can't test but I've literally just thought would adding an OnDisable() method in the custom remember script to then save the data each time the GameObject is disabled work? Then would doing the same thing for OnEnable() and load the data work? Or is this bad practice?

Apologies in advance! Thanks all.

Comments

  • It's not a stupid question at all, but your hunch is correct: the save system cannot detect Remember components attached to inactive GameObjects.

    Unity UI objects need to be disabled at the GameObject level, rather than at the Canvas level, because Unity's EventSystem is still able to detect UI components in the latter case.

    You'll have to move the Remember component off the UI, and into a separate GameObject in your scene. You can, however, rely on some scripting trickery to get a variable reference to the runtime-spawned Canvas:

    Menu myMenu = PlayerMenus.GetMenuWithName ("MyMenu");
    Canvas myCanvas = myMenu.canvas;
    
  • As ever thanks again Chris. I think I was far too concerned with the fact that all Remember scripts work on the basis that they are always attached to the GameObject where the data they need to save/load is stored - obviously thinking about it I can, as you suggest, simply move the remember script off the specific UI element and onto a separate 'always active' GameObject and simply reference the UI element to save/load onto. Huge thanks again!

  • Just a final quick question - the tablet menu I have is a child of the player and so I am going to attach the remember scripts onto some new empty gameobjects that I'll attach to the player. The plan then was to create a public gameobject variable in the remember script and then I can drag the UI gameobject into the field on the remember script so that each remember script knows which UI to reference (because I have 4 different UI elements I need to save/load data onto the example provided by yourself of searching for the menu and canvas by name inside the remember script won't work as each remember script will be referencing the same UI element).

    Problem is the public gameobject field isn't visible on the remember script - presume this is obviously by design however there are some remember scripts that do have variables exposed in the inspector (such as what the state of a trigger or collider is on start). Looks like these are always AC enums however I'm struggling to identify why these are shown in the inspector but my public gameobect variable isn't.

    Worst case I was going to rename each of the new empty gameobjects I created to match the names of the specific UI elements that I need to reference (and then change the remember script to search for the Menu element that matches the name of the gameobject it is attached to - not ideal but believe it should work).

  • Because Remember scripts require the Constant ID number to be displayed and configurable, they have a default editor script to display this as a custom UI.

    If you want to expose your custom variables as fields, you'll need to make a custom Editor script to display them in. This'll need to derive from ConstantIDEditor.

    See "RememberConversationEditor" for a super-simple example.

  • Ah ok, those editor scripts make perfect sense now. Thanks again Chris, took me a lot longer to sort out though than I'm proud to admit. I was getting very annoyed why I couldn't specify ConstantIDEditor as a subclass of my script because Unity kept telling me that it couldn't find it. The only thing left was to try to move my script into the same folder as ConstantIDEditor and surprisingly it worked (now learned that it isn't being in the same folder, just that the folder is named Editor) so I've now moved my script back out from the AC folders and into my scripts folder in a subfolder of its own called Editor. What a journey haha!

    Thanks again!

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.