Hello all, I’ve recently begun researching persistent data for saving/loading variables etc in Unity, but I’ve noticed a weird issue when trying to get this working with Unity’s improved UNET. After following the live tutorial found below, I’ve got the jist of how it all works:
The problem comes to syncing it over a network. So for starters, I’ve created a simple InputField that takes texts and creates a name for a player object. It is then sent to a persistent GameObject that floats around the game to send data over. The code for the data object looks like this at the moment:
using UnityEngine;
using System.Collections;
//using UnityEngine.Networking;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class Data : MonoBehaviour {
public static Data data;
public string playerName;
void Awake ()
{
if(data == null)
{
DontDestroyOnLoad(gameObject);
data = this;
}
else if(data != this)
{
Destroy(gameObject);
}
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData playerData = new PlayerData();
playerData.playerName = playerName;
bf.Serialize(file, playerData);
file.Close();
}
public void Load()
{
if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
PlayerData playerData = (PlayerData)bf.Deserialize(file);
file.Close();
playerData.playerName = playerName;
}
}
[Serializable]
class PlayerData
{
public string playerName;
}
}
It’s almost verbatim to the tutorial for testing purposes. It’s also worth noting that I took off the NetworkIdentity component and commented out all NetworkBehaviour code snippets as well for testing purposes. Basically what happens is that when joined within a lobby, the player names of both players appears to be whatever the local player used as their name (i.e. player X says both players as “X” and player Z sees both as “Z”). This issue happened with or without the network components/code.
Not really sure what’s going on right now, to be quite honest. I don’t have many leads, and I’m currently just fiddling with things to find out what’s going on. If anyone has any idea what’s happening, please feel free to share with me! I can post any specific code snippets or inspector screens that may be needed.
One last thing to note is that to my knowledge, it shouldn’t be an error in regards to the network code itself, as I’ve been successfully testing out my project for a good couple of months now and everything works fine with the UNET integration. I’ve just got a problem with this persistent data “overwrite”, if that makes any sense.