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 hollym16 · Feb 08, 2017 at 02:50 PM · spawnplayerprefsstringfloatsave data

Store individual positions of GameObjects in a string

I have a scene where I can spawn objects, move them around, save their positions, move around some more, then load the saved positions.

When I spawn the objects I can see which ones go into the string in the Inspector. When I try to load the saved data, they all move to one position; the last position I made before saving.

How can I make them save and revert back to their individual positions rather than one? Here is the script I'm using:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SaveScene : MonoBehaviour
 {
     public GameObject[] objectComponents;
 
     public void Update()
     {
         // Find all GameObjects in scene with the tag "clone"
         // This is in Update function so it will continually search for clone GameObjects
       objectComponents = GameObject.FindGameObjectsWithTag("Clone");
     }
     public void SavePositions()
     {
         // Apply function to all GameObjects found in the string
         foreach (GameObject component in objectComponents)
         {
             if (component.activeInHierarchy)
             {
                 // Save the x, y and z positions of the GameObjects
                 PlayerPrefs.SetFloat("X", component.transform.position.x);
                 PlayerPrefs.SetFloat("Y", component.transform.position.y);
                 PlayerPrefs.SetFloat("Z", component.transform.position.z);
                 print("Saved");
            }
         }
     }
     public void LoadPositions()
     {
         foreach (GameObject component in objectComponents)
         {
             if (component.activeInHierarchy)
             {
                 // Apply the saved positions
                 Vector3 change = component.transform.position;
                 change.x = PlayerPrefs.GetFloat("X");
                 change.y = PlayerPrefs.GetFloat("Y");
                 change.z = PlayerPrefs.GetFloat("Z");
                 component.transform.position = change;
                 print("Loaded");
             }
         }
     }
 }
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 commodore · Feb 08, 2017 at 03:04 PM 0
Share

It's a bad idea to have "FindGameObjectsWithTag" running every frame to check if any new game objects were added. I would attach a script to each of the game objects. In their Start functions, have them add themselves to a list in SaveScene.cs.

It's understandable that the positions are being set to the last one that was set. SavePositions() is just overwriting your vectors floats at key "X", "Y" and "Z". What you could do is add the index number to the key and use a for loop ins$$anonymous$$d of foreach.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ASPePeX · Feb 08, 2017 at 03:04 PM

Only the last GameObject in your array is saved since you are using the same name for each object.

If you have a different name for all the GameObjects you could for example do:

 PlayerPrefs.SetFloat(component.name + "X", component.transform.position.x);
 [...]
 change.x = PlayerPrefs.GetFloat(component.name + "X");

The name you set must be unique, otherwise it will just be overwritten.

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 Chikari · Feb 08, 2017 at 03:09 PM

PlayerPrefs is saving (Key,Value). There can only be one key. You are always saving to keys "X", "Y" and "Z". The second object then overwrites the first, the third overwrites the second and so on. That's why every object spawns at one place - the place of your last object, because previous keys were overwritten.

Your approach will thus only work when every object is of the same type and configuration. Then, instead of

 PlayerPrefs.SetFloat("X", component.transform.position.x);

you could write

 PlayerPrefs.SetFloat({UniqueID} + "X", component.transform.position.x);

so that newer keys are not overwritten. You need to create the UniqueID using a deterministic pattern, so you will be able to recreate them when loading. This will most likely not be the case though, so maybe you should look up "object serialization" and rethink your approach.

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 NoseKills · Feb 08, 2017 at 03:24 PM

All of your objects use the same PlayerPrefs keys "X", "Y" and "Z" to store their position. You can't store 10 positions using 3 numbers.

You need a way to store their positions individually. One solution would be to just index them.

 PlayerPrefs.SetInt("Count", objectComponents.Length);
 int i = 0;
 foreach (GameObject component in objectComponents)
 {
     if (component.activeInHierarchy)
     {
         // Save the x, y and z positions of the GameObjects
         PlayerPrefs.SetFloat("X"+i, component.transform.position.x);
         PlayerPrefs.SetFloat("Y"+i, component.transform.position.y);
         PlayerPrefs.SetFloat("Z"+i, component.transform.position.z);
         i++;
      ...

I don't know how exactly you want things to work but when loading, you should prepare for cases where you have saved more positions than there are objects or vice versa.

 public void LoadPositions()
 {
      int savedCount = PlayerPrefs.GetInt("Count");
      for (int i = 0; i < savedCount && i < objectComponents.Length;i++)
     {
         var component = objectComponents[i];
         if (component.activeInHierarchy)
         {
             // Apply the saved positions
             Vector3 change = component.transform.position;
             change.x = PlayerPrefs.GetFloat("X"+i);
             change.y = PlayerPrefs.GetFloat("Y"+i);
             change.z = PlayerPrefs.GetFloat("Z"+i);


There's plenty of corner cases you might need to worry about too depending on your app. Like: does the same object need to end up in the same position after loading, or is it enough that some object ends up in each of the positions. In the former case your objects need some kind of an ID and you need to save and load them based on that ID, and once again worry about things like what if object "ABC" is in the scene but theres no position saved for it and vice versa.

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.

Follow this Question

Answers Answers and Comments

68 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

Related Questions

Using Player Prefs 0 Answers

Getting a slider value from a different scene 1 Answer

Iterate through String,Float dictionary. 1 Answer

PlayerPrefs Int to Float 1 Answer

Can I create a list with an int/float and a string? C# 2 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