Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 austen 2 · Apr 05, 2011 at 05:39 PM · javascriptarrayplayerprefsload

Loading with ArrayPrefs

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[i]; var go = Instantiate(cube2, PlayerPrefsX.GetVector3(curName), Quaternion.identity); go.name = curName; Debug.Log("lOADED!"); }

 }

button2 is my saving script is in my first level, here is what the save function looks like for that level.

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 &lt; cubes.length - 1 ; i++)
     {

     curName = cubes[i];
     PlayerPrefsX.SetVector3( curName, GameObject.Find(curName).transform.position);
     }

What am i doing worng?

EDIT**

here are my new scripts.

This is my CubeManager script that is attached to empty gameobjects in the first level and the main menu.

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 &lt; cubeCount; i++)
 {
     CreateCube(PlayerPrefsX.GetVector3("cube"+i));
 }

Here is my script that is in my first level to save the cubes.

function Update() {

 var managerScript : CubeManager = FindObjectOfType(CubeManager);

     if (Input.GetMouseButtonDown(0))
         {managerScript.CreateCube(transform.position);
         }   
     if(Input.GetKey("m"))
         {

         managerScript.SaveCubes();
         Debug.Log("saved");
         }

}

Here is my script that is attached to my load button on the main menu to call the load function:

function OnMouseUp() {

var managerScript : CubeManager = FindObjectOfType(CubeManager); if(isQuit) Application.Quit(); else {

     managerScript.LoadCubes();

     }


}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Apr 05, 2011 at 06:27 PM

Are you saving your cubes array as well? If not how can you iterate through the array if it's empty ;)

Never used PlayerPrefsX yet, so i can't help you much but to get the cude count back you just have to save it. Since your cube names are just the index you can regenerate the pref-name with "cube" + i. If you want to restore the complete state, don't forget to push your cubes into your cubes array at loading.


edit

static var cubes = new List.<Transform>();

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("cubeCount", cubes.Count); for(var T : Transform in cubes) { PlayerPrefsX.SetVector3(T.name,T.position); } }

function LoadCubes() { cubes.Clear(); var cubeCount = PlayerPrefs.GetInt("cubeCount"); for(var i = 0; i < cubeCount; i++) { CreateCube(PlayerPrefsX.GetVector3("cube"+i)); } }

!!not tested!!
I just wrote that from scratch.


edit2

If this script is called "CubeManager" you can use this from any other script as long as the CubeManager is attached to a GameObject in the scene:

var managerScript : CubeManager = FindObjectOfType(CubeManager);

managerScript.LoadCubes();

ps. fixed a typing mistake the second SaveCubes should be LoadCubes of course ;)

Comment
Add comment · Show 9 · 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
avatar image austen 2 · Apr 05, 2011 at 11:53 PM 0
Share

do you know how to save to an X$$anonymous$$L file? i just need to be able to save my game

avatar image Bunny83 · Apr 06, 2011 at 12:21 AM 0
Share

Well, that depends on the platform. Web-buids can't write files, but in standalone you can use the xml classes that comes with $$anonymous$$ono: 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?

avatar image austen 2 · Apr 06, 2011 at 01:52 PM 0
Share

im 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?

avatar image Bunny83 · Apr 06, 2011 at 01:58 PM 0
Share

To 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

avatar image Bunny83 · Apr 06, 2011 at 02:05 PM 0
Share

If 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.

Show more comments

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How do to you save time for each player and display it? 2 Answers

How to get WebGL PlayerPref values out of Indexeddb? 0 Answers

displaying playerprefs 1 Answer

playerprefs problem 0 Answers

player prefs, problem but no problem??? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges