- Home /
Keep List data between 'play' tests
Hi, I have a 'manager' script on a gameObject that creates a List, 'itemList' of a custom class, 'item'. I also have a custom editor window that accesses the itemList on that gameObject and allows dynamic adding/removing/editing of the List items. The problem is that when play is pressed, the List is reset and all the items are lost.
I'm guessing the reason is to do with instancing, as the code to create the list (in the 'manager' script):
Public List<item> itemList = new List<item>();
is creating a new instance every time play is pressed.
My question is how can I make the List 'itemList' persistent between the editor and the game in play mode so that it is not reset? I'm fairly inexperienced in coding- just learning as I go along, so any help appreciated. I also am only familiar with C# at this point.
Thanks.
Answer by cjdev · Aug 05, 2015 at 07:03 AM
Sounds like you want to make your object serializable, you can do it by labeling the class and any private properties in it that you want to be persistent like this:
[System.Serializable]
public class PersistentClass
[SerializeField]
private int myVar = 0;
More info on it here.
That was exactly what I was looking for, I had seen that around but not made the connection. Works perfect, thanks!
Your answer
Follow this Question
Related Questions
Play Mode camera is not resetting to set transform. 0 Answers
Why does Game tab's scale get reset to 1 every time I click Play? 1 Answer
Gradient Changes in Editor Reset In Play Mode. Why? 1 Answer
Keyboard fails to work during play mode but OK in build. 1 Answer
Make script changes in real time (without being in play mode)? 1 Answer