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 twoface262 · Dec 12, 2012 at 04:19 AM · androidjavascriptfile-io

Saving items to a text file

I'm trying to code a saving system where it takes a variable from each item that contains the tag "planet2" and save it to a text file. I have a good ammount coded, but the problem is that when I run it, and it saves. I open the text file only to find that it is empty.

 var pla : GameObject[];
 var directory = "Saves";
 var save1 = "Saves/planets.txt";
 function Start () {
 if (!System.IO.Directory.Exists(save1) ){
       System.IO.Directory.CreateDirectory(directory);
       save();
     }
 }
 
 function Update () {
 savingtimer ++;
 if(savingtimer >= 5000){
 save();
 savingtimer = 0;
 }
 }
 
 function save(){
 if(pla == null){
 pla = GameObject.FindGameObjectsWithTag("planet2");
 }
 for(var planet2 in pla){
 planets.Add(planet2);
 }
 planets.Add (GameObject.FindGameObjectsWithTag("planet2"));
 for(var pln :GameObject in pla){
 var sw = new StreamWriter(save1);
 var pls : planetbehave = pln.GetComponent(planetbehave);
 sw.WriteLine("" + pls.pname + "\n");
 sw.Flush();
 sw.Close();
 }
   
 }
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
1
Best Answer

Answer by yoyo · Dec 12, 2012 at 06:25 AM

Looks like you are rewriting the file with each object in pla, so at most the file will nly contain the information from the last object in the list. You should move the "new StreamWriter" before the start of the loop, and the Flush/Close after the end of the loop.

You should also try connecting MonoDevelop to Unity (Run menu > Attach), set a breakpoint on the save method (put cursor on first line and press F9), and step through it (F11) to make sure it's doing what you expect. Alternatively, put a Debug.Log message as your write each line, to make sure it's actually running.

Comment
Add comment · Show 1 · 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 twoface262 · Dec 12, 2012 at 10:24 PM 0
Share

Such a simple mistake on my part. Thank you!

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

10 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

Related Questions

Load function not working propperly? 1 Answer

Loading from multiple files? 2 Answers

Android Multitouch 0 Answers

Object deformation on activat 0 Answers

About ListItems in Unity3D 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