• 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 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
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
     }
 
 }
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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

172 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 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

Loop function isn't updating 0 Answers

[SOLVED] Remove Vector3 Array step by step when GameObject spawned 1 Answer

Vector2 intersection 0 Answers

How to Raycast 2D Diagonally instead of Horizontal on a Platformer game. 0 Answers

How to set the position of these Texts ?? 0 Answers


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