Hello. I’m making an incremental game and I have a script that handles the different items you can buy and that script is on all of the items, I set the prices for each individual item in the inspector and I’m trying to save this using playerprefs.
This is what it looks like now:
public class ItemManager : MonoBehaviour {
public UnityEngine.UI.Text itemInfo;
public Click click;
public float cost;
public int tickValue;
public int count;
public string itemName;
private float baseCost;
void Awake(){
cost = PlayerPrefs.GetFloat ("ItemCost");
tickValue = PlayerPrefs.GetInt ("ItemTick");
count = PlayerPrefs.GetInt ("Amount");
}
void OnApplicationQuit(){
PlayerPrefs.SetFloat ("ItemCost", cost);
PlayerPrefs.SetInt ("ItemTick", tickValue);
PlayerPrefs.SetInt ("Amount", count);
}
}
But when I start the game all values are set to 0. Is there a solution to this or should I use a different method of saving? Thanks!
But it sets the same playerprefs in every single one.
This means that the value in the LAST item that has OnApplicationQuit called will be the ones saved. Whatever values are in that specific item will be the value in it when everything loads back up. And if it has 0 for all the values, then it’s going to be 0.
If you have the same script on all the items accessing the same variable, that doesn’t seem like it will work. Each object will need a unique variable name to keep it all straight. I don’t really see anything wrong with your save code. You might want to just get a key input or something while you are working on the script. It would probably be better to put all the objects into an array, and then save the variables from one save script, giving them a unique name. I think there may be a script on the wiki that might help with that.
I have an array of all the items in another script, could I just make a for loop to cycle through those items, and go
PlayerPrefs.SetInt(“item1” + i);
i++;
and the same thing for loading them?¨
EDIT: Made a horrible attempt at getting that to work.
The array will have to hold only the integers and floats from the objects, so you make an int array, same size as object array, and then fill it with a for loop by getting the object and the value. Then save it, and do the reverse when loading. The array script is here: http://wiki.unity3d.com/index.php/ArrayPrefs2
Ok, say you have an int which is GoldBags on each GameObject.
I don’t know what type of array you used, and that determines how you get the size of the array, but for a built in array I think you use Length:
int[] goldBagNumber = new Int[gameObjectArray.Length];
for(int i = 0; i < gameObjectArray.Length; i++){
goldBagNumber[i] = gameObjectArray[i].GetComponent<scriptName>().goldbags;
}
Then get the script I linked and read how you save an array and use that script to save it, and do the reverse to load.
You don’t want to use an Array. Array’s are unchangeable (in size). You want something more dynamic, which is why I suggested a List. List’s sizes can be changed, while array’s cannot.
You can also write your own serialize function to save all of your data to a text file. Make a string, save all of your values into that string separated by a delimiter of choice, write it to a text file. When your program runs, load the values out of the text file, separating the string into a string[ ] with the split function and converting values to numbers.
If you get good at that, you can save any kind of data. You can also choose a different directory and have more flexibility than playerprefs (if it’s not a web game). You even have the option to store the data in an external text file, for future mod support in other games
Care to get me started on that? Like how would it work saving data from the diffrerent gameobjects without having an array. Until I tried that as well, fire7side I feel like I’m close but it doesn’t work yet. I have a separate script called SaveData.cs and this is what I’ve got:
using UnityEngine;
using System.Collections;
public class SaveData : MonoBehaviour {
public GameObject[] items;
void Start(){
int[] itemLoad = new int[items.Length];
for (int i = 0; i < items.Length; i++) {
itemLoad[i] = items[i].GetComponent<UpgradeManager>().cost;
PlayerPrefsX.GetIntArray("ItemCosts");
}
}
void OnApplicationQuit(){
int[] itemSave = new int[items.Length];
for (int i = 0; i < items.Length; i++) {
itemSave[i] = items[i].GetComponent<UpgradeManager>().cost;
PlayerPrefsX.SetIntArray("ItemCosts", itemSave);
Debug.Log ("Saved: " + itemSave);
}
}
}
The gameobjects array I just dragged in my three items that I want to save data to from.
My debug says “Saved: System.Int32[ ]” three times and if I debug itemSave[i] i get “Saved: 10, 30, 60”, which is the costs of the items of the objects.
Tomnnn hinted at it. You’re going to need to be able to serialize all of this. You have a bunch of items, if I understand the original post correctly. You can create a new script (and let’s say call it ItemDatabase). In that script, you create a List of items (List). Anytime you have a new item (one that doesn’t match one in the list already) you add that item to the list. Then when it comes time to save all of this, you serialize the list. When you need to load all of it, you deserialize it. You’ll have to use Google to get your head wrapped around all of it. Without seeing ALL of your code I can’t write it for you.
I’ll see what I can do, but no promises though. I’m working on a project of my own currently. In any case, you should do some research on serializing, because even if I do write the code for you, you’ll need to be able to understand it.