Search Unity

Save a variable in external file (save/load)

Discussion in 'Scripting' started by malnar1996, Mar 5, 2011.

  1. malnar1996

    malnar1996

    Joined:
    Oct 31, 2010
    Posts:
    31
    Hello. This is probably asked many times but I searched and I can't find exactly what I want. I have five levels in my game and, right now, the player has to go from first to last level, and there he finishes it.

    I want to create an external file that will have a number inside of it (1 to 5). This can be .txt, .xml or anything else. The important thing is that unity can get a number from that file, change it and save it back.

    I hope you understand what I'm trying to say. If the file contains number 2, that means that the player passed the second level.


    Does anyone have this script (or anything similar) to share it with me? Thank you very much.
     
  2. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    normally you would use player prefs for this

    here's a quick example that creates a file and then reads from it, if you still want to use file
    Code (csharp):
    1.  
    2. function writeStuffToFile(){
    3.   var stuff : String = "stuff";
    4.   File.WriteAllText(Application.dataPath +"/stuff.txt",stuff);
    5. }
    6.  
    7. function readStufFromFile(){
    8.   var stuff : String;
    9.   stuff = File.ReadAllText(Application.dataPath +"/stuff.txt");
    10.   Debug.Log(stuff);
    11. }
    12.  
     
  3. malnar1996

    malnar1996

    Joined:
    Oct 31, 2010
    Posts:
    31
    Thank you for your reply. I tested this code (readStufFromFile) but there's a problem

    Code (csharp):
    1. function Start () {
    2. readStufFromFile();
    3. }
    4.  
    5. function readStufFromFile(){
    6.   var stuff : String;
    7.   stuff = File.ReadAllText(Application.dataPath +"/stuff.txt");
    8.   Debug.Log(stuff);
    9. }
    There's a problem with this line:
    Code (csharp):
    1. stuff = File.ReadAllText(Application.dataPath +"/stuff.txt");
    It says: "Unknown indentifier: 'File'"

    Can you please help me with this. Thank you one more time.
     
  4. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    my bad:
    you need to import System.IO
    so, it would be
    Code (csharp):
    1.  
    2. import System.IO;
    3.  
    4. function writeStuffToFile(){
    5.   var stuff : String = "stuff";
    6.   File.WriteAllText(Application.dataPath +"/stuff.txt",stuff);
    7. }
    8.  
    9. function readStufFromFile(){
    10.   var stuff : String;
    11.   stuff = File.ReadAllText(Application.dataPath +"/stuff.txt");
    12.   Debug.Log(stuff);
    13. }
    14.  
     
    DMGdesign and NicRule like this.
  5. malnar1996

    malnar1996

    Joined:
    Oct 31, 2010
    Posts:
    31
    This works!!! You can't believe how happy this makes me... Thank you very much. I went trough a lot of trouble and then you just give me a simple answer and solve all my problems. Thank you again.