Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Saving the State of Text in an Array

Discussion in 'Scripting' started by downhilldan, Jan 28, 2017.

  1. downhilldan

    downhilldan

    Joined:
    Nov 16, 2016
    Posts:
    90
    Hi All,

    I've been trying to figure this problem out for about a week now. I'm trying to save the state of text (disabled/enabled) in an array. My problem is that the script that saves and loads my data is in my first scene and then the text that I need to be disabling/enabling is in the next scene. I cant find a way to populate the array with my text from the other scene.

    My data is stored in this script along with the array of text (persists through all scenes):
    Code (CSharp):
    1. using System.Runtime.Serialization.Formatters.Binary;
    2. using System.IO;
    3. using UnityEngine.UI;
    4.  
    5. public class DataManagement : MonoBehaviour
    6. {
    7.  
    8.     public static DataManagement datamanagement;
    9.     [Space(10)]
    10.     public int currentCoins;
    11.     public int totalCoins;        
    12.     [Space(10)]
    13.     public int currentDistance;
    14.     public int totalDistance;
    15.     [Space(10)]
    16.     public int highScore;
    17.     [Space(10)]
    18.     public int modelAvailibilty = 1;
    19.  
    20.     public Text[] pricesText;   //need this array to populate with text from another scene
    21.  
    22.  
    23.     void Awake ()
    24.     {
    25.         if (PlayerPrefs.HasKey ("CharacterSelected"))
    26.         {
    27.             modelAvailibilty = PlayerPrefs.GetInt ("ModelAvailibilty");
    28.         }
    29.            
    30.         if (datamanagement == null)
    31.         {
    32.             DontDestroyOnLoad (gameObject);
    33.             datamanagement = this;
    34.         }
    35.  
    36.         else if (datamanagement != this)
    37.         {
    38.             Destroy (gameObject);
    39.         }
    40.     }
    41.  
    42.  
    43.     public void SaveData ()
    44.     {
    45.         BinaryFormatter binForm = new BinaryFormatter();
    46.         FileStream file = File.Create (Application.persistentDataPath + "/gameInfo.dat");
    47.         gameData data = new gameData();
    48.         data.highScore = highScore;
    49.         data.totalCoins = totalCoins;
    50.         data.totalDistance = totalDistance;
    51.         data.modelAvailibilty = modelAvailibilty;
    52.         data.pricesText = pricesText;
    53.         binForm.Serialize (file, data);
    54.         file.Close ();
    55.     }
    56.  
    57.  
    58.     public void LoadData ()
    59.     {
    60.         if (File.Exists (Application.persistentDataPath + "/gameInfo.dat"))
    61.         {
    62.             BinaryFormatter binForm = new BinaryFormatter ();
    63.             FileStream file = File.Open (Application.persistentDataPath + "/gameInfo.dat", FileMode.Open);
    64.             gameData data = (gameData)binForm.Deserialize (file);
    65.             file.Close ();
    66.             highScore = data.highScore;
    67.             totalCoins = data.totalCoins;
    68.             totalDistance = data.totalDistance;
    69.             modelAvailibilty = data.modelAvailibilty;
    70.             pricesText = data.pricesText;
    71.         }
    72.     }
    73. }
    74.  
    75.  
    76. [Serializable]
    77. class gameData
    78. {
    79.     public int highScore;
    80.     public int totalCoins;
    81.     public int totalDistance;
    82.     public int modelAvailibilty;
    83.     public Text[] pricesText;
    84.  
    85. }
    I'm very new to programming so any help is greatly appreciated.
    If you need any more info on what I'm trying to accomplish exactly, please let me know! :)
     
  2. vothka

    vothka

    Joined:
    Mar 27, 2015
    Posts:
    59
    there is a DontDestroyOnLoad method in Unity which prevents gameobjects to be removed from a scene on switching.
     
    downhilldan likes this.
  3. downhilldan

    downhilldan

    Joined:
    Nov 16, 2016
    Posts:
    90
    Yes, I am using that. My problem is that I don't know how to populate an array in one scene with text from another