- Home /
Saving and Loading and assinging to gameobject
Hi Guys,
I have a question about saving data. I have created a class called Gamesettings that checks how many objects are in the scene with the tag Character. It then should give each of them a random ID. This part works.
In another class i have the save and load buttons that call the functions in this script. I want to save the unique ID's of every gameobject and then when you click on load it will instantiate a prefab and apply that unique ID. But for soem reason it either just get's a new ID or It keeps on instantiating gameobjects.
Question for the future... if i can check what Unique ID a gameobject has. Can I then also give it his specific position? How would that be achieved in the best way?
using UnityEditor; using UnityEngine; using System.Collections; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO; using System.Collections.Generic;
[Serializable]
public class GameSettings : MonoBehaviour {
public static GameSettings control;
public GameObject player; //for finding the player gameobject
public Vector3 playerPosition; //for getting the players position
public static List<GameObject> characters; //= new List<GameObject> (); //getting all the characters
public static List<string> characterIDList = new List<string>(); //for setting the character ID
public GameObject characterGameObject; //reference to character prefab in resources
public GameObject[] characterArray;
//creating a list of letters and numbers for a unique ID list
private static string[] idIdentifier = new string[] {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6",
"7", "8", "9"};
//creating an Array with all possible ID's
private static ArrayList uniqueIDList = new ArrayList();
void Awake(){
//characters = new List<GameObject> ();
}
void Update(){
characterArray = GameObject.FindGameObjectsWithTag ("Character");
if (!GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<GameMenu> ().loadGame) {
CheckIfCharacterHasId ();
}
if (control == null) {
DontDestroyOnLoad (this);
control = this;
}
else if (control != this) {
Destroy(gameObject);
}
for (int i = 0; i < characterIDList.Count; i++) {
Debug.Log(characterIDList[i]);
}
Debug.Log (characterIDList.Count);
//Debug.Log (characterArray.Length);
}
public void SaveData(){
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/playerinfo.dat");
//FileStream file = File.Create (Application.persistentDataPath + "/" + GameMenu.saveGameName + ".dat");
PlayerData data = new PlayerData ();
data.UniqueID = characterIDList;
//data.characterCount = characters.Count;
data.characterCount = characterArray.Length;
for (int i = 0; i < data.UniqueID.Count; i++) {
Debug.Log(data.UniqueID[i]);
}
bf.Serialize (file, data);
file.Close ();
}
public void LoadData(){
if (File.Exists (Application.persistentDataPath + "/playerinfo.dat")) {
BinaryFormatter bf = new BinaryFormatter ();
//FileStream file = File.Open(Application.persistentDataPath + "/" + GameMenu.saveGameName + ".dat", FileMode.Open);
FileStream file = File.Open (Application.persistentDataPath + "/playerinfo.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize (file);
Debug.Log ("Before loaded ID" + characterIDList.Count);
characterIDList = data.UniqueID;
Debug.Log ("After loaded ID" + characterIDList.Count);
file.Close ();
foreach (string iD in characterIDList) {
characterGameObject = Instantiate (Resources.Load ("Character", typeof(GameObject))) as GameObject; //creates a new clone of the prefab
}
for(int i = 0; i < characterArray.Length; i++){
characterArray[i].GetComponent<EntityScript>().characterID = characterIDList[i];
}
}
}
public static string getID() {
string uniqueID;
do{
uniqueID = "";
for(int i = 0; i < 8; i++) {
uniqueID += idIdentifier[UnityEngine.Random.Range(0, idIdentifier.Length)];
}
} while(uniqueIDList.Contains(uniqueID));
uniqueIDList.Add(uniqueID);
return uniqueID;
}
void CheckIfCharacterHasId(){
for (int i = 0; i < characterArray.Length; i++) {
if(characterArray[i].GetComponent<EntityScript>().characterID == ""){
characterArray[i].GetComponent<EntityScript>().characterID = getID();
}
if(characterArray[i].GetComponent<EntityScript>().characterID != ""){
if (!characterIDList.Contains(characterArray[i].GetComponent<EntityScript>().characterID)){
characterIDList.Add(characterArray[i].GetComponent<EntityScript>().characterID);
}
}
}
}
}
[Serializable]
class PlayerData{
public List<string> UniqueID = new List<string>();
public int characterCount;
}
Your answer
Follow this Question
Related Questions
Loading a list into unity 0 Answers
How to save data with the webplayer avaiable to all users? 1 Answer
Save/Load Objects settings(position, rotation ...) 1 Answer
Save and Load a Game 1 Answer