- Home /
Save Datasets at runtime
Hello everyone,
I have been working on an application that stores contacts (just a fun project) and I am having trouble saving and then loading data in the application. What I need to do is be able to create gameobject clones at runtime(I can do this already), save those clones, and load them in the exact same spot with the same variables(name, telephone, address, etc.). So far I have played around with arrays, playerprefs, the whole deal, but keep running into problems. Can someone explain a synopsis(for lack of a better word) of how such a script would act, and maybe point me towards code I could use? Thank you, and good day.
Wolfshadow
Answer by sleepandpancakes · Apr 17, 2017 at 10:42 PM
I found this link helpful: http://nielson.io/2015/09/saving-games-in-unity/
@sleepandpancakes did you actually use this, and if so, would it address my problems? It looks like it was a fancier way to do playerprefs.
Yes I used this for my game, because it seemed to make more sense and be more flexible than PlayerPrefs. It works perfectly for me. That being said, I've never actually used PlayerPrefs :P
Answer by NextVertex · Apr 18, 2017 at 11:54 AM
There are many possibilities to save/load data, but For your case, i think that you can use PlayerPref, it's the easiest way to save Data, but it's limited.
I Suggest you to save a prefab like EmptyContact (which contain the default contact image, the text for example MyName, ... (common to all contacts) ) and then, when you clone them, a script attached to this prefab will load the data saved by using PlayerPref.
I've created an application (personnal project too) and i had the EXACT same problem
You need to save, (for example before application exit), the number of contact there is. For example,
GameObject[] ContactList = GameObject.FindGameObjectsWithTag("Contact");
int AmountOfContact = ContactList.lenght;
PlayerPref.SetInt("AmountContact",AmountOfContact);
I've created a script that is on each contact clone, for example, ContactData.cs
Start the script by writing this :
public Text ContactCloneTitle;
public Text ContactClonePhoneNumber;
public Image ContactCloneImg;
As you can see, Text/image are NOT static, because EACH contact has his own Data.
Then, when your game start, just Load the data that we will define bellow.
To save the Data, use PlayerPref. Name your Pref for example "ContactCloneTitle"+IndexOfThisContact to have always a name different depending of the different clone.
Then , when you load the data, just use :
ContactCloneName.text = PlayerPref.GetString("Contact"+IndexOfThisContact+"Name");
ContactClonePhoneNumber.text = ...
Of course, this code is just an example. If your number of Contact is too high, this solution isn't the best, but this worked for me.