Cant save/load game data
Well im "making" a game so i can practice and i want to save my game stats ive been looking alot on the internet but couldnt actually save this game data i dont know why. I know the code should be simple so if you can give me an idea or a code i would be very happy !!
using UnityEngine;
using System.Collections;
[Serializable]
public class Storage : MonoBehaviour {
//money
public static int money = 0 ;
public static int moneymultiplier = 1;
public static int moneymultipliercost = 50000;
//stats&hp
public static int health = 0;
public static int RatHelath = 50;
public static int DogHealth = 200;
public static int ChickenHealth = 600;
public static int HumanHealath = 1500;
public static int BossHealth = 100000;
public static int TapDMG = 1;
public static bool SpriteIsTriggered = false;
public static int Rats = 0;
public static int Dogs = 0;
public static int Chicken = 0;
public static int Human = 0;
public static int boss = 0;
//hunger
public static int RatsHunger = 5;
public static int DogsHunger = 10;
public static int ChickenHunger = 15;
public static int HumanHunger = 20;
public static int bossHunger = 50;
//fed
public static int RatFed = 0;
public static int DogFed = 0;
public static int ChickenFed = 0;
public static int HumanFed = 0;
public static int BossFed = 0;
//unlock sprites
public static bool Ratunlocked = false;
public static bool Dogunlocked = false;
public static bool Catunlocked = false;
public static bool Humanunlocked = false;
public static bool bossunlocked = false;
//cup
public static int CupDMG = 75;
public static int CupUpgrade = 1;
public static int CupUpgradePrice = 5000;
//level
public static int Level = 0;
public static int exp = 0;
public static int needed = 50;
}
Check this out on how to write and load custom data files from disc, as for the playerPrefs if you going to save a couple values it should suffice and it is easier but I highly recommend to spend some time learning the methods above. Cheers.
Answer by RealGamesStudio · Apr 20, 2017 at 12:41 PM
Why dont you use PlayerPrefs? Check out this link - https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
Answer by nikoyal · Apr 21, 2017 at 09:22 AM
Tried this
using UnityEngine;
using System.Collections;
using System;
public class SaveGame : MonoBehaviour {
// Use this for initialization
public void Save () {
PlayerPrefs.SetInt ("Money", Storage.money);
}
// Update is called once per frame
public void Load () {
Storage.money = PlayerPrefs.GetInt ("Money");
}
}
and this
using UnityEngine;
using System.Collections;
using System;
public class SaveGame : MonoBehaviour {
// Use this for initialization
public void Save () {
PlayerPrefs.SetInt ("Money", Storage.money);
}
// Update is called once per frame
public void Load () {
Storage.money = PlayerPrefs.GetInt ("Money");
}
}
It didnt wok :(
Your answer
Follow this Question
Related Questions
[CLOSED]Problem with serialization after PUN update 2 Answers
Cannot read past end of stream, MemoryStream 1 Answer
How to write a Story Event System with ScriptableObjects? 0 Answers
can not open project stuck on white screen 0 Answers
How to serialize a List of ContentPacks (from Morph3D) in Photon Unity Networking? 0 Answers