Question by 
               saR · Mar 27, 2019 at 04:51 PM · 
                playerprefsvector2for-loopstring formatting  
              
 
              How can I set the positions by numbers stored in a string ?
Hello , so I've a loop that adds positions of objects to a string , it looks like this :
 for (int n = 0; n < posicionesVector2.Length; n++)
             {
                 for (int i = 0; i < posicionesVector2.Length; i++)
                 {
                     counter++;
                     posicionesVector2[i].x = GameObject.Find("Quad" + counter).GetComponent<Bloque>().coordenada.x;
                     posicionesVector2[n].y = GameObject.Find("Quad" + counter).GetComponent<Bloque>().coordenada.y;
                     listaVectores.Add(GameObject.Find("Quad" + counter).transform.position);
                     s += string.Format("{0}:{0} ", posicionesVector2[i].x, posicionesVector2[n].y);
                     PlayerPrefs.SetString("PosicionsBloques", s);
                 }
             }
So later on on my "load game" loop I want to set the coordenada.x and coordenada.y with what's stored in the string.
This line:
 s += string.Format("{0}:{0} ", posicionesVector2[i].x, posicionesVector2[n].y);
Is storing the values correctly but when I want to load them they're not correct.
I load them like this :
 for (int z = 0; z < posBlocks.Length; z++)
                 {
                     for (int i = 0; i < posBlocks.Length; i++)
                     {
                         contar++;
                         GameObject objeto = GameObject.Find("Quad" + contar);
                         objeto.transform.position = new Vector3(posicionX[i].x, posicionX[z].y, 0);
                         GameObject.Find("Quad" + contar).GetComponent<Bloque>().coordenada = new Vector2Int(s[i], s[z+1]);
                     }
                 }
I dont know what I'm doing wrong. Thank you!
               Comment
              
 
               
              Answer by KittyAnn · Apr 13, 2019 at 12:06 PM
Here is my save position script. I have it attached to my player. I have save position & load position buttons in the top right of the screen for the player to save their favorite place in the game any time they want. I hope this helps!
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SavePosition : MonoBehaviour
 {
     public void SavePlayerPosition()
     {
         PlayerPrefs.SetFloat("PlayerX", transform.position.x);
         PlayerPrefs.SetFloat("PlayerY", transform.position.y);
         PlayerPrefs.SetFloat("PlayerZ", transform.position.z);
     }
 
     public void LoadPlayerPosition()
     {
         float x = PlayerPrefs.GetFloat("PlayerX");
         float y = PlayerPrefs.GetFloat("PlayerY");
         float z = PlayerPrefs.GetFloat("PlayerZ");
 
         transform.position = new Vector3(x, y, z);
     }
 
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                