• 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 COLLAnitySV · Jan 12, 2012 at 09:55 AM · arrayvector3playerprefsfor-loopplayerprefsx

assigning arrays with for in

hi, i have a problem with assigning arrays with for in loops. I'm going to make PlayerPrefsX.SetVector3Array with this. How to make it saves / assign the Arrays of Vector3 in RegSaver Vector3 arrays (which saves the arrays) ? But, when i'm trying to make the for(var om in rs.objPos) transform.position = om; and yea it's really does working but when i check the RegSaver arrays, all values of Vector3 were same. You know what i mean right ? Please. thanks for the read

this is my script. (objects.js) rs is RegSaver;

     if(PlayerPrefs.HasKey("plx")){
             positions = PlayerPrefsX.GetVector3Array("plx");
             for(var om in rs.objPos){
                 transform.position = om;
             }
         }
     function Update () {
     if(Input.GetKeyDown("b")) SaveExitGame();
     for(var om in rs.objPos){
         om = transform.position;
     }
 }

and RegSaver.js

 var objPos : Vector3[];
 var usedObj : boolean[];
 
 function Update(){
     if(Input.GetKeyDown("p")){
         SaveThisPrg();
     }
 }
 
 function LoadThisPrg(){
     objPos = PlayerPrefsX.GetVector3Array("plx");
     usedObj = PlayerPrefsX.GetBoolArray("pbx");
 }
 
 function SaveThisPrg () {
     PlayerPrefsX.SetVector3Array("plx",objPos);
 }
Comment
Add comment · Show 3
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 Kryptos · Jan 12, 2012 at 10:22 AM 1
Share

What I don't understand is why you are saving an array of Vector3, when you only need one Vector3 to store/retrieve the position ?

avatar image COLLAnitySV · Jan 12, 2012 at 01:40 PM 0
Share

it's for saving the game objects location when the Application Quit...

avatar image syclamoth · Jan 12, 2012 at 02:24 PM 0
Share

Right. As explained in my post, that's really not what you're doing in that script there.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by syclamoth · Jan 12, 2012 at 10:22 AM

Well, this is all very well- but what do you really expect?

 transform.position;

will always return one value, and by the looks of things you are iterating through an array and setting every single member of the array to the same thing! If you look at the Update function, you'll see that every single frame you overwrite the entire array with a single value. Why do you expect transform.position to return something different in every iteration?

Of course, even the other end of the system is flawed. When you read the values back out again, even if the array has a lot of different values in it, only the last one will make any difference! Because you're setting 'transform.position', none of the previous iterations will be remembered outside of the loop! Only the last one actually matters.

So, unless there's something you're not telling us about what this script is supposed to do, I'm not really sure how you could expect it to do anything different!

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

For In Loop Fills all values in arrays problem 2 Answers

Saving Array Objects in Android. 0 Answers

How to save rotation with PlayerPrefsX? 1 Answer

PlayerPrefsX plugin error when build for windows store 3 Answers

Attempting to store an Inventory Array in PlayerPrefsX 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges