what im trying to do is add a save load feature into my game, i want to save the location fo all the cubes in the game. i'm using the arrayprefs script from http://www.unifycommunity.com/wiki/index.php?title=ArrayPrefs. i have my save function working but i cant figure out how to load the game from my main menu scene. Here is my load function. this script is in my main menu.
function loadGame()
{
Application.LoadLevel("start");
for (i=0 ; i < button2.cubes.length ; i++)
{
curName = button2.cubes*;*
*var go = Instantiate(cube2, PlayerPrefsX.GetVector3(curName), Quaternion.identity);*
*go.name = curName;*
*Debug.Log("lOADED!");*
*}*
*}*
*```*
*<p>button2 is my saving script is in my first level, here is what the save function looks like for that level. </p>*
*```*
*static var cubes = new Array();*
*var cubeCount = 0;*
*function Update()*
*{*
*if (Input.GetMouseButtonDown(0))*
*{*
*var go = Instantiate(cube2, transform.position, Quaternion.identity) as GameObject;*
*go.name = "cube"+cubeCount;*
*cubes.Add("cube"+cubeCount);*
*cubeCount++;*
*}*
*if(Input.GetKey("m"))*
*{*
*saveGame();*
*Debug.Log("saved");*
*}*
*}*
*function saveGame()*
*{*
*for (i=0 ; i < cubes.length - 1 ; i++)*
*{*
_curName = cubes*;*_
_*PlayerPrefsX.SetVector3( curName, GameObject.Find(curName).transform.position);*_
_*}*_
_*```*_
_*<p>What am i doing worng? </p>*_
<em>_<p>EDIT**</p>_</em>
_*<p>here are my new scripts. </p>*_
_*<p>This is my CubeManager script that is attached to empty gameobjects in the first level and the main menu.</p>*_
_*```*_
_*static var cubes = new List.<Transform>();*_
_*var cube2 : GameObject;*_
_*function CreateCube(position : Vector3) : Transform*_
_*{*_
_*var cube : GameObject = Instantiate(cube2, position, Quaternion.identity);*_
_*cube.name = "cube" + cubes.Count;*_
_*cubes.Add(cube.transform);*_
_*}*_
_*function SaveCubes()*_
_*{*_
_*PlayerPrefs.SetInt("button2.cubeCount", cubes.Count);*_
_*for(var T : Transform in cubes)*_
_*{*_
_*PlayerPrefsX.SetVector3(T.name,T.position);*_
_*}*_
_*}*_
_*function LoadCubes()*_
_*{*_
_*Application.LoadLevel("start");*_
_*cubes.Clear();*_
_*var cubeCount = PlayerPrefs.GetInt("button2.cubeCount");*_
_*for(var i = 0; i < cubeCount; i++)*_
_*{*_
_*CreateCube(PlayerPrefsX.GetVector3("cube"+i));*_
_*}*_
_*```*_
_*<p>Here is my script that is in my first level to save the cubes.</p>*_
_*```*_
_*function Update()*_
_*{*_
_*var managerScript : CubeManager = FindObjectOfType(CubeManager);*_
_*if (Input.GetMouseButtonDown(0))*_
_*{managerScript.CreateCube(transform.position);*_
_*}*_
_*if(Input.GetKey("m"))*_
_*{*_
_*managerScript.SaveCubes();*_
_*Debug.Log("saved");*_
_*}*_
_*}*_
_*```*_
_*<p>Here is my script that is attached to my load button on the main menu to call the load function:</p>*_
_*```*_
_*function OnMouseUp()*_
_*{*_
_*var managerScript : CubeManager = FindObjectOfType(CubeManager);*_
_*if(isQuit)*_
_*Application.Quit();*_
_*else*_
_*{*_
_*managerScript.LoadCubes();*_
_*}*_
_*}*_
_*```*_
do you know how to save to an XML file? i just need to be able to save my game
– anon95971066Well, that depends on the platform. Web-buids can't write files, but in standalone you can use the xml classes that comes with Mono: http://www.go-mono.com/docs/index.aspx?link=T%3ASystem.Xml.XmlWriter I haven't used it yet so i can't go in detail here, but why don't you use the PlayerPrefs(X)? I can post an example if you want. You need only the positions of the cubes, right?
– Bunny83im trying to use the LoadCubes function outside of my scene (in the main menu), so i can load from the main menu. but when i try to make the createcube function static, it gives me an error because cube2 is not satic but if i make it static i cant assign a prefab to it. how do i load frorm the main menu?
– anon95971066To what object is that script attached to? All that stuff like Createcube / saveing / loading and storing is a global task so you should place it on a manager object that is already in the scene
– Bunny83If you have this script somewhere in the scene you just have to find the GameObject and use GetComponent to get your script reference. Then you can call any public function. If there is only one instance of this script you can use FindObjectOfType to get the script reference directly. I can add an example to my answer.
– Bunny83