- Home /
How to prevent a list from being cleared at runtime?
In the editor I'm building lists attached to C# scripts on various gameObjects. Those lists are getting cleared at the start of the game. I need to preserve the data they contain...
Arrays don't have this problem - that is, I can populate arrays in-editor, and the data persists as the game runs. But lists are vastly better suited to my use case.
Anyone know why lists are getting cleared on start, but arrays aren't?
Answer by Languard · Jun 27, 2015 at 08:32 PM
Edit: Make sure you are calling EditorUtility.SetDirty on the changed object. This lets the editor know that a script has changed the value of an object (or really anything has changed the value of an object) and that it needs to be serialized out to disk.
Verify that you are not creating the lists in the Start method. A common error is to do this:
List<int> somelist;
void Start()
{
somelist = new List<int>();
}
This will allow you to populate the list in the editor, but of course as soon as you run the game...boom! Data gone. Instead, do it this way:
List<int> someList;
void Start()
{
if(someList == null)
{
someList = new List<int>();
}
}
This way if it wasn't set in the editor it won't crash, but if it was set in the editor it won't overwrite the data.
Hm - doesn't work for me.
I'm creating the list like so:
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class LODSystemSwap : $$anonymous$$onoBehaviour { public List systemsBlocks; ....
And I'm populating that list using a separate script in the editor which runs only while I'm interactive in the editor. Here's the relevant populator: if (!system.GetComponent().systemsBlocks.Contains(castleBlock)){ system.GetComponent().systemsBlocks.Add(castleBlock); } ... That makes a nice tidy list.
When I hit the "play" button, blammo, that list vaporizes.
I've tried adding this to the script that declares the list: void Start () { if (systemsBlocks == null) { systemsBlocks = new List(); }
but that doesn't solve the problem.
Any idea what I might do at this point? Thanks either way.
oh ffs - the web page scrambled my example. Not sure how to keep it readable... sorry.
$$anonymous$$aybe it's because the formatting ate it, but I don't see where you actually create the list. public List systemsBlocks; would not create it, only declare it. The only thing I can suggest at this point is to hunt for any stray systemBlocks = new List() lines you might have.
I'm only creating the list in the editor as a piece of a C# script. I don't create it elsewhere in the project.
Curiously, it had appeared that arrays would persist between the editor and running the game - but I've now realized a different aspect of the problem. If I manually enter a value in a public variable, in editor, the field goes bold, and that field will persist into the execution of the game (even if subsequent modifications of that value happen by executing an editor script.)
If, however, I populate public variables only via editor script, those values do not persist on execution - nor do they ever go bold-face in the gui. Presumably I need to trick Unity into thinking I've modified each field I want to keep.
Any ideas?
Thanks either way.
Inspector values override script values. This is probably why it works.
Yep - just wondering if there's a way to procedurally make the inspector store those values... Looking now.
Answer by mgc90403 · Jun 28, 2015 at 07:01 PM
This isn't a great solution, but it appears that after I populate the fields of the array with an editor script, I can then "lock them in" by going to the component (in the inspector) and selecting "copy component" then "paste component values". That will cause all entered fields to go bold-facce, and likewise to persist when the game runs.
Seems like some kind of bug where the editor doesn't treat fields edited by script as first-class-citizens, so to speak.
I'd certainly welcome feedback or suggestions to make this cleaner...