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 saR · Mar 24, 2019 at 11:10 AM · playerprefsserialization

How can I save a series of instantiated objects from an array?

Hi, I'm creating a slider game for mobile. My problem is that when the player makes progress and moves the squares around I want to save the position of each square , the puzzle instantiates when the scene starts (its on the start function, what it does it creates a 3x3 and divides the image in quads, so each quad is a piece of the image). I don't really understand how to save this type of objects, I've managed to learn how to save the points,the level and the time, but not the position of the objects. The way I can imagine it could be done is by traveling through the array then store each position every time an object is moved but I dont know how to do it. They are Vector2Int if you need more information I can provide.

Thanks in advance :)

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

2 Replies

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

Answer by qobion · Mar 24, 2019 at 12:15 PM

If you want to save vectors or arrays in playerprefs you can convert it to string and save this string with some formatting so then you can convert it again to array back. This is look like this:

     Vector2Int [] positions;
 
     void SavePositions()
     {
         string s = "";
         foreach (Vector2Int c in positions)
         {
             s += string.Format("{0}:{1}:",c.x,c.y);
         }
         PlayerPrefs.SetString("MyPositions", s);
     }
 
     void LoadPositions()
     {
         string s = PlayerPrefs.GetString("MyPositions");
         if (s == null)
             return;
         string[] p = s.Split(':');
         for (int i = 0; i < p.Length; i+=2)
         {
             positions[i].x = System.Convert.ToInt32(p[i]);
             positions[i].y = System.Convert.ToInt32(p[i+1]);
         }
     }

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
avatar image
0

Answer by saR · Mar 24, 2019 at 01:30 PM

Thank you for your answer @qobion ! I think I understood the code you've written. So what I should do is save each position by having a public static Vector2Int on the blocks script so I save each position and then replace it in the code you just said.

The thing is that I dont really understand where to replace it , I'm so sorry , I just finished my first year with c# and in the code you just said there's some syntaxis I dont understand, but anyways , thank you for your response I really apreciate it !

Comment
Add comment · Show 3 · 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
avatar image qobion · Mar 24, 2019 at 09:06 PM 1
Share

This is how you can save Array of Vector2Int.

If you want to save single Vector2

     void SavePosition()
     {
         string s = string.Format("{0}:{1}", transform.position.x, transform.position.y);
         PlayerPrefs.SetString(gameObject.name, s);
     }
 
     void LoadPosition()
     {
         string s = PlayerPrefs.GetString(gameObject.name);
         if (s == null)
             return;
         string[] p = s.Split(':');
 
         Vector2 position = new Vector2(float.Parse(p[1]), float.Parse(p[2]));
     }

avatar image saR qobion · Mar 27, 2019 at 01:56 PM 0
Share

$$anonymous$$y problem now is that is saving much more than I need , here's my saving script:

 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
         {
             listaVectores.Clear();
             string s = "";
             Puzzle._bloqueVacio.gameObject.SetActive(true);
             int contador = 0;
             int[,] testeo = new int[Puzzle.tamañoPuzzle,Puzzle.tamañoPuzzle];
 
             for (int h = 0; h < Puzzle.tamañoPuzzle; h++)
             {
                 for (int i = 0; i < Puzzle.tamañoPuzzle; i++)
                 {
                     contador++;
                     int b = GameObject.Find("Quad" + contador).GetComponent<Bloque>().coordenada.x;
                     int a = GameObject.Find("Quad" + contador).GetComponent<Bloque>().coordenada.y;
                     testeo[i, h] = b;
                     print("Posiciones x:" + b);
                     print("Posiciones y:" + a);
                     listaVectores.Add(GameObject.Find("Quad" + contador).transform.position);
                     s += string.Format("{0}:{0}", b, a);
                     PlayerPrefs.SetString("PosicionsBloques", s);
                 }
                 
                 
                 
             }
             
             SaveGame.Save("posicionesVectores.dat", listaVectores);
             print("s length: "+s.Length); 
             print("Playerprefs posicions bloques: " +PlayerPrefs.GetString("PosicionsBloques"));
         }

I dont know why it's saving only positions 0 1 2 and not saving 3 4 5 , 6 7 8 , Puzzle._tamañoPuzzle is 9 in this case. I Debug.Log the s.length and it end's up at 27 , also the coordinates are wrong , the vector3 are not but the int ones of a and b are wrong , I cant find the error :( .

avatar image saR · Mar 25, 2019 at 04:05 AM 0
Share

Thank you!

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

110 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

Related Questions

Save/Load Animation State of Instantiated Prefabs 0 Answers

Best Way to Store Large Number of GameObjects? 1 Answer

How to save a game 1 Answer

Serialization 2 Answers

Serializing PlayerPrefs 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