• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Worempie · Sep 25, 2015 at 12:31 PM · gameobjectsaveloaddata

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;
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Save and Load Function 2 Answers

Hard question about saving and loading 1 Answer

Saving/Loading data to Windows Phone 8 3 Answers

Save Multiple GameObject with XML 1 Answer

How to save/load references to components and prefabs 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges