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 Scacchi · Apr 20, 2013 at 08:26 PM · gameobjectsaveloadsystemvalue

Hard question about saving and loading

Hello, Hope you are doing good. I have a system for creating a fortress in my game, I have 8 Gameobjects, and inside those 8 GameObjects, I have like 23 more GameObjects inside each one, each one of these objects has a script that contains a variable called "Cubo" (Cube in Spanish). I want to know a way of saving the value of "Cubo" in each one, and then, load them properly, but I cant figure out how, does anybody know how to do this?

Here is an image so you can see more crearly what is happening:

alt text

So, Here you can see the GameObjects "Fila1", "Fila2", etc go fro Up to Down, not from left to right.

So, anybody knows how yo do this?

jpeg.png (485.2 kB)
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
Best Answer

Answer by Howey-Do-It · Apr 20, 2013 at 10:52 PM

For saving/loading you should look into Player Preferences, specifically, PlayerPrefs.SetString/GetString.

docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.SetString.html docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.GetString.html

Sorry, the hyperlink button isn't working on my browser.

I tested it and if I understand correctly this C# code should work:

 using UnityEngine;
 using System.Collections;
 
 public class SaveLoad : MonoBehaviour {
     
     public string[] loadedData;
     public int convertBack;
     
     // Update is called once per frame
     void Update () 
     {
     
         // Easy way to test it press "S" in game to save and "L" to load
         
         if(Input.GetKeyDown("s"))
             SaveCubos();
         
         if(Input.GetKeyDown("l"))
             LoadCubos();
         
     }
     
     public void SaveCubos()
     {
         GameObject[] cubos = GameObject.FindGameObjectsWithTag("Cubos"); // If you create a new tag "Cubos" and tag all of the cubos with it
         string saveData = "";
         
         foreach(GameObject specificCubo in cubos)
         {
             EspacioCreacion cuboScript = specificCubo.GetComponent<EspacioCreacion>();
             
             saveData = saveData + cuboScript.cubo.ToString() + ","; // The additional comma is for splitting the string later
         }
         Debug.Log(saveData); // To display what information we actually saved
         PlayerPrefs.SetString("SaveData", saveData); // Actually save the information to PlayerPrefs
     }
     
     public void LoadCubos()
     {    
         string loadData = PlayerPrefs.GetString("SaveData"); // Retrieve the information we saved
         
         loadedData = loadData.Split(','); // Divides the information up based on the commas "," into a string array
         
         // To convert the information back into integers or floats you could use:
         convertBack = int.Parse(loadedData[0]);
     }
 }

I guess it matters whether you want to assign the data back to a specific cube. If so you will probably need to save more information that associates the "cubos" with each specific cube. You will also need to tag each object with the "Cubos" tag.

I hope that answers your question.

Thanks, God bless!

Howey

Comment
Add comment · Show 6 · 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 Scacchi · Apr 21, 2013 at 05:54 AM 0
Share

Its good but the problem is that this saves the info randomly, I need it in order so then I can load it in order, how can I do that?

avatar image Howey-Do-It · Apr 21, 2013 at 07:19 AM 0
Share

Are the cubes static? Do they ever change in a way that would affect the saving process? Does the number of them ever shrink/increase? If any of these are true it is going to be more complex, if not it should be pretty simple.

avatar image Scacchi · Apr 21, 2013 at 07:42 AM 0
Share

I made it more simple, I erased all the "Fila1", "Fila2", "Fila3", etc... I also erased the "Espacio1", "Espacio2", "Espacio3", etc... I made one big Object with 160 GameObjects named from "1" to "160", that way is easier to sort them in a List, and to save them, I got what I wanted, thanks a lot my friend!

(However If you think there is a more optimized way to do this let me know please, the users are first!)

avatar image Howey-Do-It · Apr 21, 2013 at 02:12 PM 0
Share

Alright, awesome! I just want to make sure you are set up properly. Did you figure out how to order them in the list properly?

[EDIT]: Sorry, I didn't read your comment very well. Unless you already figured this out, you could cycle through each of those 1-160 objects using an INT - STRING conversion and vice versa, adding 1 each time you check. But you probably already know that.

Have great one!

God bless!

Howey

avatar image Scacchi · Apr 22, 2013 at 06:27 PM 0
Share

Thats why I did exactly, here is the code so people can use it if they need it...

 public List<GameObject> Cubos;
     
     void Start() {
         Cubos = new List<GameObject>(); 
         GameObject[] go = GameObject.FindGameObjectsWithTag("Cubo");
         foreach(GameObject Cubito in go) {
             Cubos.Add(Cubito);
         }
         Cubos.Sort(
             delegate(GameObject i1, GameObject i2) 
             { 
                return int.Parse(i1.name).CompareTo(int.Parse(i2.name));
             }
         );
     }

And then I save and load them with the functions you wrote, thanks a lot, couldnt have done it without you! Have a nice day ;)

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting error on instatiating. 0 Answers

How to save/load references to components and prefabs 1 Answer

Scene Saving 1 Answer

Save/Load Objects settings(position, rotation ...) 1 Answer

simple checkpoint/save/load system in javascript 4 Answers


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