Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mgc90403 · Jun 27, 2015 at 08:04 PM · arraylistlists

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mgc90403 · Jun 27, 2015 at 08:44 PM 0
Share

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.

avatar image mgc90403 mgc90403 · Jun 27, 2015 at 08:46 PM 0
Share

oh ffs - the web page scrambled my example. Not sure how to keep it readable... sorry.

avatar image Languard · Jun 27, 2015 at 09:00 PM 0
Share

$$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.

avatar image mgc90403 · Jun 28, 2015 at 06:36 PM 0
Share

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.

avatar image meat5000 ♦ · Jun 28, 2015 at 07:09 PM 0
Share

Inspector values override script values. This is probably why it works.

avatar image mgc90403 · Jun 28, 2015 at 07:24 PM 0
Share

Yep - just wondering if there's a way to procedurally make the inspector store those values... Looking now.

Show more comments
avatar image
1

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to return index of List element? 1 Answer

Is there a way to remove array entries in the editor? 4 Answers

List of bools assigned to each Gameobject in List/Array 0 Answers

A node in a childnode? 1 Answer

Reading all data in the list with duplicate data inside 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges