- Home /
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);
}
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 ?
it's for saving the game objects location when the Application Quit...
Right. As explained in my post, that's really not what you're doing in that script there.
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!