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 TehBuddha · May 15, 2013 at 07:58 PM · arrayssavestringloadtext file

Issues with string array taken from a .txt file

Im currently trying to code a save state for my project. So far Ive managed to get the file written, read to an array, then set to variables in another script. However, although the string variable for "Owner" appears correctly in the inspector (It should be "AAA", which it is). Im not too sure why this is. Debug.Log shows the string as "AAA", but Owner.Length returns 4 characters, which is also odd.

Here is the code for the read/write script.

 import System.IO;
 
 //FileWritingVariables
 var savedirectory : String = "assets/savedgames/";
 var savename : String = "SaveGame1.txt";
 var LoadingSave : boolean = true;
 
 //LoadingVariables
 var savefile : TextAsset;
 var PlanetSaveData = new Array();
 
 //PlanetVariables
     //LargePlanets
 var LP01 : GameObject;
     var    LP01Pop = 0;
     var LP01Name : String = "LP1";
     var LP01Owner : String;
 var LP02 : GameObject;
     var LP02Pop = 0;
     var LP02Name : String = "LP2";
     var LP02Owner : String;
 var LP03 : GameObject;
     var LP03Pop = 0;
     var LP03Name : String = "LP3";
     var LP03Owner : String;
     //SmallPlanets
     //Regions
 
 function Start () {
     Debug.Log("Save Key is bound to F12");
     //Loading Save File to String Array//
     if (LoadingSave == true){
         var linearray : String[] = savefile.text.Split("\n"[0]);
             for (var thisLine : String in linearray){
                 PlanetSaveData.Push(thisLine);
                 }        
     }
 }
 
 function Update () {
     if (Input.GetKeyDown(KeyCode.F12)){
         //Defining Planets//
         LP01 = GameObject.Find("LP01");
             LP01Pop = LP01.GetComponent(PlanetData).Population;
             LP01Name = LP01.GetComponent(PlanetData).Name;
             LP01Owner = LP01.GetComponent(PlanetData).Owner;
         LP02 = GameObject.Find("LP02");
             LP02Pop = LP02.GetComponent(PlanetData).Population;
             LP02Name = LP02.GetComponent(PlanetData).Name;
             LP02Owner = LP02.GetComponent(PlanetData).Owner;
         LP03 = GameObject.Find("LP03");
             LP03Pop = LP03.GetComponent(PlanetData).Population;
             LP03Name = LP03.GetComponent(PlanetData).Name;
             LP03Owner = LP03.GetComponent(PlanetData).Owner;
         
         //Start Writing//
         sw = new StreamWriter(savedirectory + savename);
         sw.WriteLine(LP01Name);
             sw.WriteLine(LP01Pop);
             sw.WriteLine(LP01Owner);
         sw.WriteLine(LP02Name);
             sw.WriteLine(LP02Pop);
             sw.WriteLine(LP02Owner);
         sw.WriteLine(LP03Name);
             sw.WriteLine(LP03Pop);
             sw.WriteLine(LP03Owner);
         sw.Close();
         Debug.Log("GameData Saved");
     }
 }
 

This is the code that references it and puts it into variables:

 import System.IO;
 
 var Name : String = "MajPlanet1";
 var Population = 10000;
 var Owner : String;
 
 var PlanetTag : String;
 
 var StatsHolder : GameObject;
 
 var OwnerFloat = 0;
 var UpdateOwner : boolean = false;
 var SaveArray;
 
 
 function Awake(){
     StatsHolder = GameObject.Find("StatsHolder");
     }
 
 function Start(){      
 //Loading From SaveData//
  
 var LoadingSave : boolean = StatsHolder.GetComponent(WriteScript).LoadingSave;
     if (LoadingSave == true){
         yield WaitForSeconds (1);    //Need to wait for array to form in writescript
         SaveArray = StatsHolder.GetComponent(WriteScript).PlanetSaveData;
             //Setting Planet Data//
                 if (PlanetTag == "LP01"){
                     var LP01Pop : String = SaveArray[1];
                     Name = SaveArray[0];
                     Population = System.Int32.Parse(LP01Pop);
                     Owner = SaveArray[2];
                     }
                 if (PlanetTag == "LP02"){
                     var LP02Pop : String = SaveArray[4];
                     Name = SaveArray[3];
                     Population = System.Int32.Parse(LP02Pop);
                     Owner = SaveArray[5];
                     }
                 if (PlanetTag == "LP03"){
                     var LP03Pop : String = SaveArray[7];
                     Name = SaveArray[6];
                     Population = System.Int32.Parse(LP03Pop);
                     Owner = SaveArray[8];
                     }
                 //Done Setting Planet Data//
         }
         
     Debug.Log (Owner.Length);
 }
 
 
 function Update(){
         if (Owner == "AAA"){
              OwnerFloat = 1;
              }
         if (Owner == "BBB"){
              OwnerFloat = 2;        
              }
         if (Owner == "CCC"){
              OwnerFloat = 3;
              }
         else{
             OwnerFloat = 5;
             }
 }

And this is how the text file Im reading from currently looks

 NotEuthalia
 2000000
 AAA
 Aleria
 43563
 BBB
 AlmostAssuredlyNotSilaari
 124
 CCC
 SomeString
 

PS, I know my code is probably insanely messy, fairly new to all of this.

Comment
Add comment · Show 2
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 Dave-Carlile · May 15, 2013 at 08:00 PM 0
Share

In your text file, are there spaces after the text? If the text was "AAA " you'd get a length of 4.

avatar image TehBuddha · May 15, 2013 at 08:20 PM 0
Share

Nope, no spaces. Copied the text up there directly from the file

2 Replies

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

Answer by pazzesco · May 15, 2013 at 08:30 PM

Very possibly line endings. Try changing your code from this...

 Owner = SaveArray[2];


To this...

 Owner = SaveArray[2].Trim();


You'll want to change that for all instances. Might eliminate that extra white space, if that's the issue.

Comment
Add comment · Show 3 · 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 code-blep · May 15, 2013 at 08:37 PM 0
Share

Either way I would investigate to find out exactly what character is causing it rather than just stripping out, if that is indeed the problem.Good idea pazzesco :)

avatar image pazzesco · May 15, 2013 at 09:21 PM 0
Share

I agree. Definitely look into why those line endings are showing up that way, if that's the case. Understanding a bug or problem fully is always a good practice. You don't want to have a program "work" by coincidence. And thank you, UFO Hunter.

avatar image TehBuddha · May 15, 2013 at 10:45 PM 0
Share

Ah, that was it. Opened it up in Notepad++ and it had line ending characters. Thanks, and thanks to UFO Hunter too.

avatar image
1

Answer by code-blep · May 15, 2013 at 08:24 PM

How about hidden characters like line endings? I use Notepad++ to expose stuff like that.

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

16 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

Related Questions

PlayerPrefs script problem 2 Answers

save string from array to player prefs 1 Answer

Get the order of scenes from a text file 0 Answers

Why can't I save an array? 1 Answer

Unity c# StreamReader to list always returns false. 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