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
2
Question by hirenkacha · Jan 15, 2014 at 10:13 AM · c#listlifetime

C# List elements lifetime

I just used List of string to store words from json file. I parsed the json and stored the values in the List. My script looks like this.

 public List <string> a = new List<string>();
 
 void Start()
 {
     //JSON Parsing 
     var jd = JSONNode.Parse(jsonString);
     print (jd.Count);
     for(int no=0; no<jd["A"].Count;no++)
     {
         a.Add(jd["A"][no].Value);
     }
     print ("A => "+a.Count);
 }

If I have 10 values from json, it is added to the List a. I get the print "A => 10". When I stop and run my project again my start method again does parsing and adds value to List a. But my List count is now 20. And if I run again, it will be 30 and so on. I tried it on device also. On device after uninstalling and again installing, I get it added to the still get the count as 10. Is it always necessary to clear() the List in the Start() to make the count 0? If I am not doing Clear() before adding strings to List, it always keeps previous values even after stopping the app on editor and on device also.

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

1 Reply

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

Answer by Bunny83 · Jan 15, 2014 at 10:31 AM

Do you have the attribute ExecuteInEditMode on your class? That would run Start during edittime and add the strings to the list in edit mode. Since your List "a" is public it would be serialized in the editor so the values are preserved. If your setup looks like this you should either removed ExecuteInEditMode (which causes a lot such problems) or make the List private or use the NonSerialized attribute on the List variable.

If you use SimpleJSON your way of processing the object is very inefficient. You should do something like:

 var jd = JSONNode.Parse(jsonString);
 foreach(var N in jd["A"].Childs)
 {
     a.Add(N.Value);
 }

or if you want / need to use an indexed loop, do it like this:

 var jd = JSONNode.Parse(jsonString);
 var a = jd["A"];
 for(int no=0; no<a.Count; no++)
 {
     a.Add(a[no].Value);
 }

Objects are stored in a dictionary, so each access with ["A"] have to search the entry in the dict. Dictionary are quire optimised but still require some iterations. In your code you access jd["A"] two times per iteration.

Comment
Add comment · Show 2 · 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 hirenkacha · Jan 15, 2014 at 11:22 AM 0
Share

@Bunny83, Yes, I am using ExecuteInEdit$$anonymous$$ode. Is that making things worst?

avatar image Bunny83 · Jan 15, 2014 at 01:44 PM 1
Share

If you use ExecuteInEdit$$anonymous$$ode you should understand what it does. It let the script execute while you're in edit mode. Any changes the script does to the scene / project will be permanent since you work on the editor copy.

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

19 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

A node in a childnode? 1 Answer

How do you get a certain index of a List 2 Answers

Unity stops responding when i ask it to execute the same function for the third time 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