I'm working in Unity to make a game and I'm having some problems with a Save System.
What I want is to save variables from more than 1 script.
- In [GameManager.cs] i'm saving things like Level, Currency and Scores, all of that in one file.
- In [MenuManager.cs] I want to save the user name IN ANOTHER file (in case he didn't did it yet) using the same saving system.
- Acces those variables in other scripts. Example: I want to Log the variables saved in point 1) (GameManager.cs) in point 2) (MenuManager.cs). Or ... is possible to load(READ them from the saved file) them from [MenuManager.cs] using the same script from [GameManager.cs]? (I managed to load them only from [GameManager.cs] using the LoadPlayer() function).
Here is how 1st application run look like: https://imgur.com/a/IqoQjpX and the second: https://imgur.com/a/QpienFQ
The question is why in second photo it outputs blank? Why the save system works perfectly fine for [GameManager.cs] and for [MenuGame.cs] it doesn't?
Here s how the script looks:
[PlayerData]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class PlayerData
{
public int Score;
public int BestScore;
public string UserName;
// 1st Constructor
public PlayerData(GameManager Player)
{
BestScore = Player.BestScore;
}
// 2nd Constructor
public PlayerData(MenuManager User)
{
UserName = User.UserName;
}
}
[SaveSystem.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class SaveSystem
{
public static void SavePlayer(GameManager Player)
{
BinaryFormatter formatter = new BinaryFormatter();
string Path = Application.persistentDataPath + "/player.xd";
FileStream Stream = new FileStream(Path, FileMode.Create);
PlayerData Data = new PlayerData(Player);
formatter.Serialize(Stream, Data);
Stream.Close();
}
public static void SavePlayer(MenuManager Player)
{
BinaryFormatter formatter = new BinaryFormatter();
string Path = Application.persistentDataPath + "/name.xd";
FileStream Stream = new FileStream(Path, FileMode.Create);
PlayerData Data = new PlayerData(Player);
formatter.Serialize(Stream, Data);
Stream.Close();
}
public static PlayerData LoadPlayer()
{
string Path = Application.persistentDataPath + "/player.xd";
if (File.Exists(Path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream Stream = new FileStream(Path, FileMode.Open);
PlayerData Data = formatter.Deserialize(Stream) as PlayerData;
Stream.Close();
return Data;
}
else
{
return null;
}
}
public static PlayerData LoadName()
{
string Path = Application.persistentDataPath + "/name.xd";
if (File.Exists(Path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream Stream = new FileStream(Path, FileMode.Open);
PlayerData Data = formatter.Deserialize(Stream) as PlayerData;
Stream.Close();
return Data;
}
else
{
return null;
}
}
}
[MenuManager.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class MenuManager : MonoBehaviour
{
public string UserName = "whereIsTheLogic";
public InputField input;
public void Start()
{
// Here I checked if the user saved his name
PlayerData Data = SaveSystem.LoadName();
if (Data == null)
{
Debug.Log("User didn't saved his name");
/* so ... We jump to SaveButton() */
}
else
{
Debug.Log("User saved his name");
/* The file exists ... so we assume this isn't his first application use, right? */
/* Now what I want is to simply log the name ... just log already 4 god sake!! */
UserName = Data.UserName;
Debug.Log("User saved name is: " + UserName);
/* BUT it outputs blank NOT null or the inserted name) */
}
}
public void SaveButton()
{
string userName = input.text;
SaveSystem.SavePlayer(this);
Debug.Log("The user name: " + userName); /* yes ... it outputs the correct user answer */
}
public void PlayGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
[GameManager.cs] (only things that matter ...)
public class GameManager : MonoBehaviour
{
public int Score = 0;
public int BestScore = 0;
void Start()
{
/* Here I load saved variables (Score/Best Score)
and it loads them correctly
*/
LoadPlayer();
}
public void SavePlayer()
{
// Here I save the above mentioned variables and ... yes it works correctly
SaveSystem.SavePlayer(this);
Debug.Log("Salvat");
}
public void LoadPlayer()
{
PlayerData Data = SaveSystem.LoadPlayer();
Debug.Log("Loaded");
BestScore = Data.BestScore;
}
}