x


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

asked Jan 12 '12 at 09:55 AM

COLLAnitySV gravatar image

COLLAnitySV
36 21 23 25

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 ?

Jan 12 '12 at 10:22 AM Kryptos

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

Jan 12 '12 at 01:40 PM COLLAnitySV

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

Jan 12 '12 at 02:24 PM syclamoth
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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!

more ▼

answered Jan 12 '12 at 10:22 AM

syclamoth gravatar image

syclamoth
15k 7 15 80

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1397
x601
x301
x97
x12

asked: Jan 12 '12 at 09:55 AM

Seen: 601 times

Last Updated: Jan 12 '12 at 02:24 PM