- Home /
How to save Item from List in Unity
I've some problem with save Item from List<>. So I've 3 script: Item script, Save script and PlayerData script. Not for List I've not problem, but I don't understand how to make it to List or serializable class.
1.
public class ItemActivatedController : MonoBehaviour
{
[System.Serializable]
public class Items
{
public string name;
public GameObject item;
public bool active;
}
public List<Items> ItemTable = new List<Items>();
//some conditions
2.
public static class SaveSystem
{
public static void SavePlayer(GameScript gameScript,PopUpShopScene popupScript)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.data";
FileStream stream = new FileStream(path, FileMode.Create);
PlayerData data = new PlayerData (gameScript, popupScript);
formatter.Serialize(stream, data);
stream.Close();
}
public static PlayerData LoadPlayer()
{
string path = Application.persistentDataPath + "/player.data";
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
{
Debug.LogError("Save file not found in " + path);
return null;
}
}
3.
[System.Serializable]
public class PlayerData
{
public double CoinsV;
public double CrystalV;
public DateTime TommorowDateV;
public double UsedStepsV;
public double NewStepsV;
public PlayerData(GameScript gameScript, PopUpShopScene popupScript)
{
CoinsV = GameScript.Coins;
TommorowDateV = GameScript.TomorrowDate;
UsedStepsV = GameScript.UsedSteps;
NewStepsV = GameScript.NewStepsValue;
CrystalV = GameScript.Crystal;
Debug.Log(CrystalV);
}
}
How to save all Item params from List ItemTable to my PlayerData script?
Answer by Ellie97 · Jun 18, 2019 at 12:30 PM
I'd create a list in PlayerData, variable type ItemActivatedController.Item, then get a reference to ItemTable and set the new list in PlayerData to ItemTable. You can get a reference with the standard GetComponent<> functions or through the inspector. There are also ways with singletons (I think that's what they're called) where you create a static variable of the class you want to reference. Let me know if you want an example of that.
Could you write these 2 ways on the example of my code, since it is difficult for me to understand because English is not my native language, and in the code I will understand it. thank you in advance
Sure, try this. It's the singleton approach I mentioned. Using the reference variable I created, you can access the ItemActivatedController script anywhere by using ItemActivatedController.reference.VariableOrFunctionYouWantToAccess. This also means there is only ever one instance of that script. Apologies if any of that is unclear or difficult to understand!
public class ItemActivatedController : $$anonymous$$onoBehaviour
{
//NEW CODE//
public static ItemActivatedController reference;
[System.Serializable]
public class Items
{
public string name;
public GameObject item;
public bool active;
}
public List<Items> ItemTable = new List<Items>();
//NEW CODE//
private void Awake()
{
if (reference == null)
reference = this;
}
}
[System.Serializable]
public class PlayerData
{
public double CoinsV;
public double CrystalV;
public DateTime $$anonymous$$morowDateV;
public double UsedStepsV;
public double NewStepsV;
public PlayerData(GameScript gameScript, PopUpShopScene popupScript)
{
CoinsV = GameScript.Coins;
$$anonymous$$morowDateV = GameScript.$$anonymous$$orrowDate;
UsedStepsV = GameScript.UsedSteps;
NewStepsV = GameScript.NewStepsValue;
CrystalV = GameScript.Crystal;
Debug.Log(CrystalV);
}
//NEW CODE//
List<Items> PlayerItemTable = ItemActivatedController.reference.ItemTable;
}
Answer by Sal1mus · Jun 18, 2019 at 03:23 PM
Thank u. I've problem in PlayerData script in this part
List<Items> PlayerItemTable = ItemActivatedController.reference.ItemTable;
PlayerData script:
public PlayerData(GameScript gameScript, PopUpShopScene popupScript, ItemActivatedController itemControllerActive)
{
CoinsV = GameScript.Coins;
TommorowDateV = GameScript.TomorrowDate;
UsedStepsV = GameScript.UsedSteps;
NewStepsV = GameScript.NewStepsValue;
CrystalV = GameScript.Crystal;
Debug.Log(CrystalV);
Test = Script.ItemTable;
}
List<Items> PlayerItemTable = ItemActivatedController.reference.ItemTable;
Error:
PlayerData.cs(31,10): error CS0246: The type or namespace name 'Items' could not be found (are you missing a using directive or an assembly reference?)
Maybe this is due to the fact that the List is in the ItemActivatedController script, and the line List PlayerItemTable = ItemActivatedController.reference.ItemTable; in the PlayerData script?
Your answer
Follow this Question
Related Questions
How do i give Coordinates (x, y) to an Array 1 Answer
Ways to make OnMouseDown (or a single script) differentiate between different colliders/sprites 1 Answer
c# - List with multiple types - Trading Engine - Stock Simulator 2 Answers
variable not being changed outside of a method. [C#] 3 Answers
Trigger Sound more than Once 0 Answers