Quantcast
Channel: Questions in topic: "playerprefs"
Viewing all articles
Browse latest Browse all 1333

Convert my playerpref back to obj

$
0
0
![alt text][1]So 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; } } [1]: /storage/temp/111463-desktop-2-17-2018-3-45-00-am-847.jpg

Viewing all articles
Browse latest Browse all 1333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>