Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Writing variables in external files

Discussion in 'Scripting' started by dredknight, Sep 3, 2020.

  1. dredknight

    dredknight

    Joined:
    Sep 6, 2018
    Posts:
    37
    Hello all,

    I got 2 scenes between which I want to transfer a variable. I found a couple of ways to do that, my question is how to do it by writing a file from one file and reading it with the other?

    I got something like this so far:
    Code (CSharp):
    1. string lgCode = "Somestring value";
    2. byte[] bytes = Encoding.ASCII.GetBytes(lgCode);
    3. System.IO.File.WriteAllBytes("../lg.xml", bytes);
    Now I got 2 questions, how to read from that file from a script in the othe scene?
    and how to know where is the location the script is in?

    Can you give me some best practices for finding the path? reading from file and migrating the byte to string?

    OR

    Just a better way to save the var when the app is shutdown

    OR just a better way to migrate vars between scenes.

    Thank you!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,777
    Better way to migrate vars between scenes: a static variable pointing to a class holding your data:
    Code (csharp):
    1. [System.Serializable]
    2. public class SomeData {
    3. public string foo;
    4. }
    5.  
    6. // elsewhere
    7. public class SomeGameManager : MonoBehaviour {
    8. public static SomeData thisGameData;
    9.  
    10. void Awake() {
    11. thisGameData = new SomeData() { foo = "Somestring value" };
    12. }
    13. }
    14.  
    15. // elsewhere
    16. public class SomeOtherSceneScript : MonoBehaviour {
    17. void Start() {
    18. Debug.Log($"The value is {SomeGameManager.thisGameData != null ? SomeGameManager.thisGameData.foo : "NULL"}");
    19. }
    20. }
    Better way to save data to disk: JsonUtility

    Code (csharp):
    1. //this is why I put the data above in a class with System.Serializable
    2. public class SaverLoader : MonoBehaviour {
    3. public void SaveData() {
    4. string json = JsonUtility.ToJson(SomeGameManager.thisGameData);
    5. System.IO.File.WriteAllText( Path.Combine(Application.persistentDataPath, "someFile.json"), json);
    6. }
    7.  
    8. public void LoadData() {
    9. string json = System.IO.File.ReadAllText(Path.Combine(Application.persistentDataPath, "someFile.json"));
    10. SomeGameManager.thisGameData = JsonUtility.FromJson<SomeData>(json);
    11. }
    12. }
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    Well just the opposite of what you've done so far...
    https://docs.microsoft.com/en-us/dotnet/api/system.io.file.readallbytes?view=netcore-3.1

    Have some constant name for the file, and put it in in this folder: path: https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html That's important because the location of a reliably writeable folder will be different per operating system, and that property will give you such a folder.
    If you just need data to persist between scenes and not between when the game is shut down and started back up, you can use https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html to keep an object around between scenes, or use static variables.
     
  4. dredknight

    dredknight

    Joined:
    Sep 6, 2018
    Posts:
    37
    Thank you both of you! So much to dig in :)!