• 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 /
  • Help Room /
avatar image
0
Question by darkal · Feb 17, 2018 at 08:09 AM · playerprefsloadconvertsave data

Convert my playerpref back to obj

alt textSo i have it set up to save my objects with the positions to player pref but i need help converting it from string it saves as back to object and transform.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ItemDB : MonoBehaviour {
 
     public bool save;
     public bool load;
 
     public GameObject[] ItemPrefabs;
     public GameObject[] TerrianDB;
     public GameObject[] ObjsDB;
     public Vector3[] ObjPos;
     public string[] saved;
     public string[] savedname;
     public string[] savedpos;
     public int savedlength;
     // Use this for initialization
     void Start() {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         TerrianDB = GameObject.FindGameObjectsWithTag("TerrianObj");
         ObjsDB = GameObject.FindGameObjectsWithTag("Obj");
         ObjPos = new Vector3[ObjsDB.Length];
 
 
         for (int i = 0; i < ObjsDB.Length; i++)
         {
             ObjPos[i] = ObjsDB[i].transform.position;
         }
         if (load)
         {
             LoadDB();
         }
         if (save)
         {
             SaveDB();
         }
     }
 
     void LoadDB()
     {
         savedlength = PlayerPrefs.GetInt("Num");
         saved = new string[savedlength];
         savedname = new string[savedlength];
         savedpos = new string[savedlength];
         for (int i = 0; i < savedlength; i++)
         {
             saved[i] = PlayerPrefs.GetString("Data" + i.ToString());
             savedname[i] = PlayerPrefs.GetString("Data1" + i.ToString());
             savedpos[i] = PlayerPrefs.GetString("Data2" + i.ToString());
         }
         Spawn();
         load = false;
     }
 
     void Spawn()
     {
 
     }
 
     void SaveDB()
     {
         saved = new string[ObjsDB.Length];
         savedname = new string[ObjsDB.Length];
         savedpos = new string[ObjsDB.Length];
         PlayerPrefs.SetInt("Num", ObjsDB.Length);
 
         for (int i = 0; i < ObjsDB.Length; i++)
         {         
             PlayerPrefs.SetString("Data" + i.ToString(), ObjsDB[i].name + ":" + ObjPos[i].ToString());
             PlayerPrefs.SetString("Data1" + i.ToString(), ObjsDB[i].name);
             PlayerPrefs.SetString("Data2" + i.ToString(), ObjPos[i].ToString());
         }
         save = false;
     }
 }


desktop-2-17-2018-3-45-00-am-847.jpg (422.7 kB)
Comment
Add comment · Show 1
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
avatar image darkal · Feb 17, 2018 at 09:15 AM 0
Share

so i figured out how save the vector but still need help converting the object.name back to gameobject

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class ItemDB : $$anonymous$$onoBehaviour {
  
      public bool save;
      public bool load;
  
      public GameObject[] ItemPrefabs;
      public GameObject[] TerrianDB;
      public GameObject[] ObjsDB;
      public Vector3[] ObjPos;
      public string[] saved;
      public string[] savedname;
      public Vector3[] savedvec;
      public int savedlength;
  
      // Use this for initialization
      void Start() {
  
      }
  
      // Update is called once per frame
      void Update()
      {
          TerrianDB = GameObject.FindGameObjectsWithTag("TerrianObj");
          ObjsDB = GameObject.FindGameObjectsWithTag("Obj");
          ObjPos = new Vector3[ObjsDB.Length];
  
  
          for (int i = 0; i < ObjsDB.Length; i++)
          {
              ObjPos[i] = ObjsDB[i].transform.position;
          }
          if (load)
          {
              LoadDB();
          }
          if (save)
          {
              SaveDB();
          }
      }
  
      void LoadDB()
      {
          savedlength = PlayerPrefs.GetInt("Num");
          saved = new string[savedlength];
          savedname = new string[savedlength];
          savedvec = new Vector3[savedlength];
          for (int i = 0; i < savedlength; i++)
          {
              saved[i] = PlayerPrefs.GetString("Data" + i.ToString());
              savedname[i] = PlayerPrefs.GetString("Data1" + i.ToString());
              savedvec[i].x = PlayerPrefs.GetFloat("Datax" + i.ToString());
              savedvec[i].y = PlayerPrefs.GetFloat("Datay" + i.ToString());
              savedvec[i].z = PlayerPrefs.GetFloat("Dataz" + i.ToString());
          }
          Spawn();
          load = false;
      }
  
      void Spawn()
      {
  
      }
  
      void SaveDB()
      {
          PlayerPrefs.SetInt("Num", ObjsDB.Length);
  
          for (int i = 0; i < ObjsDB.Length; i++)
          {         
              PlayerPrefs.SetString("Data" + i.ToString(), ObjsDB[i].name + ":" + ObjPos[i].ToString());
              PlayerPrefs.SetString("Data1" + i.ToString(), ObjsDB[i].name);
              PlayerPrefs.SetFloat("Datax" + i.ToString(), ObjPos[i].x);
              PlayerPrefs.SetFloat("Datay" + i.ToString(), ObjPos[i].y);
              PlayerPrefs.SetFloat("Dataz" + i.ToString(), ObjPos[i].z);
          }
          save = false;
      }
  }

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by darkal · Feb 17, 2018 at 09:47 AM

So after trail and lots of Errors i figured out how do it with out having make separate database.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ItemDB : MonoBehaviour {
 
     public bool save;
     public bool load;
 
     public GameObject[] ItemPrefabs;
     public GameObject[] TerrianDB;
     public GameObject[] ObjsDB;
     public Vector3[] ObjPos;
     public string[] saved;
     public string[] savedname;
     public Vector3[] savedvec;
     public GameObject[] spawnobj;
     public int savedlength;
 
     // Use this for initialization
     void Start() {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         TerrianDB = GameObject.FindGameObjectsWithTag("TerrianObj");
         ObjsDB = GameObject.FindGameObjectsWithTag("Obj");
         ObjPos = new Vector3[ObjsDB.Length];
 
 
         for (int i = 0; i < ObjsDB.Length; i++)
         {
             ObjPos[i] = ObjsDB[i].transform.position;
         }
         if (load)
         {
             LoadDB();
         }
         if (save)
         {
             SaveDB();
         }
     }
 
     void LoadDB()
     {
         savedlength = PlayerPrefs.GetInt("Num");
         saved = new string[savedlength];
         savedname = new string[savedlength];
         savedvec = new Vector3[savedlength];
         spawnobj = new GameObject[savedlength];
         for (int i = 0; i < savedlength; i++)
         {
             saved[i] = PlayerPrefs.GetString("Data" + i.ToString());
             savedname[i] = PlayerPrefs.GetString("Data1" + i.ToString());
             savedvec[i].x = PlayerPrefs.GetFloat("Datax" + i.ToString());
             savedvec[i].y = PlayerPrefs.GetFloat("Datay" + i.ToString());
             savedvec[i].z = PlayerPrefs.GetFloat("Dataz" + i.ToString());
         }
         for(int i = 0; i < ItemPrefabs.Length; i++)
         {
             for (int x = 0; x < savedname.Length; x++)
             {
                 if (ItemPrefabs[i].name == savedname[x].Replace("(" + x.ToString() + ")" ,"").Trim())
                 {
                     spawnobj[x] = ItemPrefabs[i];
                 }
             }
         }
         Spawn();
         load = false;
     }
 
     void Spawn()
     {
 
     }
 
     void SaveDB()
     {
         PlayerPrefs.SetInt("Num", ObjsDB.Length);
 
         for (int i = 0; i < ObjsDB.Length; i++)
         {         
             PlayerPrefs.SetString("Data" + i.ToString(), ObjsDB[i].name + ":" + ObjPos[i].ToString());
             PlayerPrefs.SetString("Data1" + i.ToString(), ObjsDB[i].name);
             PlayerPrefs.SetFloat("Datax" + i.ToString(), ObjPos[i].x);
             PlayerPrefs.SetFloat("Datay" + i.ToString(), ObjPos[i].y);
             PlayerPrefs.SetFloat("Dataz" + i.ToString(), ObjPos[i].z);
         }
         save = false;
     }
 }
 
Comment
Add comment · Share
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

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

128 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

PlayerPrefs help ! 1 Answer

Saving In Between Scenes and Program Exit 0 Answers

Using PlayerPrefs to remember transform heirarchies? 0 Answers

Save instances and recognize them on load 0 Answers

create store database class for RTS games 0 Answers

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