Saving the State of Text in an Array

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):

using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine.UI;

public class DataManagement : MonoBehaviour
{

    public static DataManagement datamanagement;
    [Space(10)]
    public int currentCoins;
    public int totalCoins;        
    [Space(10)]
    public int currentDistance;
    public int totalDistance;
    [Space(10)]
    public int highScore;
    [Space(10)]
    public int modelAvailibilty = 1;

    public Text[] pricesText;   //need this array to populate with text from another scene


    void Awake ()
    {
        if (PlayerPrefs.HasKey ("CharacterSelected"))
        {
            modelAvailibilty = PlayerPrefs.GetInt ("ModelAvailibilty");
        }
           
        if (datamanagement == null)
        {
            DontDestroyOnLoad (gameObject);
            datamanagement = this;
        }

        else if (datamanagement != this)
        {
            Destroy (gameObject);
        }
    }


    public void SaveData ()
    {
        BinaryFormatter binForm = new BinaryFormatter();
        FileStream file = File.Create (Application.persistentDataPath + "/gameInfo.dat");
        gameData data = new gameData();
        data.highScore = highScore;
        data.totalCoins = totalCoins;
        data.totalDistance = totalDistance;
        data.modelAvailibilty = modelAvailibilty;
        data.pricesText = pricesText;
        binForm.Serialize (file, data);
        file.Close ();
    }


    public void LoadData ()
    {
        if (File.Exists (Application.persistentDataPath + "/gameInfo.dat"))
        {
            BinaryFormatter binForm = new BinaryFormatter ();
            FileStream file = File.Open (Application.persistentDataPath + "/gameInfo.dat", FileMode.Open);
            gameData data = (gameData)binForm.Deserialize (file);
            file.Close ();
            highScore = data.highScore;
            totalCoins = data.totalCoins;
            totalDistance = data.totalDistance;
            modelAvailibilty = data.modelAvailibilty;
            pricesText = data.pricesText;
        }
    }
}


[Serializable]
class gameData
{
    public int highScore;
    public int totalCoins;
    public int totalDistance;
    public int modelAvailibilty;
    public Text[] pricesText;

}

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! :slight_smile:

there is a DontDestroyOnLoad method in Unity which prevents gameobjects to be removed from a scene on switching.

1 Like

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