Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Loading a list into unity 0 Answers

How to save data with the webplayer avaiable to all users? 1 Answer

Error when loading saved date : SerializationException: End of Stream encountered before parsing was completed. 2 Answers

Save/Load Objects settings(position, rotation ...) 1 Answer

Save and Load a Game 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges