Playerprefs not saving Guids
So, I have a problem where I am trying to save characters in player prefs. I am trying to save them as a list in one. I want to ID of the character to be a Guid, but it seems to not save it.
Player Structure public class Player { public Guid id;
public Player(string _name, bool isNew, PlayerData _player)
{
if (isNew)
{
id = Guid.NewGuid();
}
}
(I have a lot more in my original structure but i am reducing for easier reading) PlayerData structure (for saves)
public class PlayerData
{
public Guid id;
public PlayerData (Player player)
{
id = player.id;
}
}
Creation
public static void CreateCharacter (Player player)
{
Text txtResults = GameObject.Find("CharacterCreationResultsText").GetComponent<Text>();
Debug.Log(player.name);
PlayerData playerData = new PlayerData(player);
Debug.Log(playerData.id);
Saves data = CharacterList();
// Checks if a character already exists
if (CheckSumCharacter(playerData, data))
return;
// Adds in the character to the list, and saves it.
data.saves.Add(playerData);
PlayerPrefs.SetString("characters", JsonUtility.ToJson(data));
PlayerPrefs.Save();
txtResults.color = Color.green;
txtResults.text = "Character Creation Successful!";
//MainMenu.AddCharacter(player.id);
}
Saves Structure
using System.Collections.Generic;
[System.Serializable]
public class Saves
{
public List<PlayerData> saves = new List<PlayerData>();
}
I am pretty stumped on why this is happening. So either it cant save Guids out of the box, or I am missing something.
EDIT: Actually it seems the normal JsonUtility built into unity isn't saving parsing it.
Answer by Bloodmorph · Oct 29, 2019 at 01:19 AM
Well... I feel a bit dumb but all I did was convert the Guid into a string in my constructor so i handle it all as a string and not a guid.
This fixed everything, posting for anyone else is:
Guid.NewGuid();
Should be
Guid.NewGuid().ToString();
Then declaring and comparing as a string. I feel a bit dumb trying to do it all as Guid but.. you live and you learn.