Save game state problem

i’m going mad trying to get this to work and i know i’m doing something really dumb, probably the strings but i just can’t find it. Can anyone put me out of misery?
i’ve confirmed that the array pointer is correct but i’m getting ‘1’ back for all float values. I don’t think this is even the default.

this is part of my save game state code:

function OnApplicationQuit ()
{
	var n = 1;
	for (var piece in pieces) 
    {
    	var pieceName = "P" + n.ToString ();    	
    	valueName = pieceName + "posX";	
    	PlayerPrefs.SetFloat("valueName", piece.transform.position.x); 	
    	valueName = pieceName + "posY";
    	PlayerPrefs.SetFloat("valueName", piece.transform.position.y);
    	valueName = pieceName + "posZ";   	
    	PlayerPrefs.SetFloat("valueName", piece.transform.position.z);   	   	
    	valueName = pieceName + "rotX";
    	PlayerPrefs.SetFloat("valueName", piece.transform.eulerAngles.x);
    	valueName = pieceName + "rotY";
    	PlayerPrefs.SetFloat("valueName", piece.transform.eulerAngles.y);
    	valueName = pieceName + "rotZ";
    	PlayerPrefs.SetFloat("valueName", piece.transform.eulerAngles.z);	  	
    	n++;
    } 		
}

and this to restore:

function Restore ()
{
	var n = 1;
	for (var piece in pieces) 
    {
    	var pieceName = "P" + n.ToString (); 	
    	valueName = pieceName + "posX";
    	piece.transform.position.x = PlayerPrefs.GetFloat("valueName");  	   	
    	valueName = pieceName + "posY";
    	piece.transform.position.y = PlayerPrefs.GetFloat("valueName");
    	valueName = pieceName + "posZ";
    	piece.transform.position.z = PlayerPrefs.GetFloat("valueName");	
    	valueName = pieceName + "rotX";
    	piece.transform.eulerAngles.x = PlayerPrefs.GetFloat("valueName");
    	valueName = pieceName + "rotY";
    	piece.transform.eulerAngles.y = PlayerPrefs.GetFloat("valueName");
    	valueName = pieceName + "rotZ";
    	piece.transform.eulerAngles.z = PlayerPrefs.GetFloat("valueName");  	
    	n++;
    }
}

your problems comes from valueName.

use:
PlayerPrefs.SetFloat(valueName, piece.transform.position.x);
instead of

PlayerPrefs.SetFloat("valueName", piece.transform.position.x);

The reason is because “valueName” is a string and using valueName is using the content of the variable.

of course, silly me, thanks

as a follow on from that i just wondered if when you create an array of GameObjects using gameObject.FindGameObjectsWithTag the element order is aways the same i’ve never seen any variation but since i’m relying on it for my game state restoration i’d like to sure.

it is not granted to be the same. if you require a specific order you must store them in an array within a component.

you mean as in drag and drop from the assets or hierachy window onto the array in the inspector?