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 Ochreous · Jun 11, 2012 at 12:01 AM · c#loadingsavingvalues

Unity 3d C# saving and Loading Values

Hello everyone, I have a question. How do you save and load values in C#? I need to save a boolean, integer, and float.

To be a bit more specific I would like to save it to a file through either XML or JSON Serialization.

Comment
Add comment · Show 2
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 IGoByChad · Jun 11, 2012 at 12:08 AM 0
Share

Please be more specific. You want to save them to a file? Do you just need them on a Game Object? Depending on the answer, there are vastly different ways to do this.

avatar image Ochreous · Jun 11, 2012 at 08:19 PM 0
Share

Yes I would like to save it to a file. I've Heard that people use JSON or X$$anonymous$$L but I don't know how to set them up.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Berenger · Jun 11, 2012 at 12:08 AM

It depends what you mean by saving those values. If it is at runtime, just use a boolean, an integer and a float.

 bool myBoolean;
 int myInt;
 float myFloat;


If you need to modify them outside of their scope and still have the modification applied, you need an object, class or struct.

 public struct (or class) MyStruct
 {
     public bool myBoolean;
     public int myInt;
     public float myFloat;
 }
 // Then, elswhere
 MyStruct myStruct = new MyStruct();
 
 // Then, elswhere again
 myStruct.myInt++;


If you want to save them between scenes, you can use PlayerPrefs, static var or a gameObject with DontDestroyOnLoad.


Finally, if you want to save them before leaving the game and load them after it is restarted, the data must be saved on your computer. PlayerPrefs would still work, or you can take a look at xml serialization.

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 Ochreous · Jun 11, 2012 at 08:17 PM 0
Share

How do you setup an X$$anonymous$$L serialization and how do you get it to save and load values?

avatar image Berenger · Jun 11, 2012 at 11:45 PM 0
Share

If you go with that, take a look here or here.

avatar image Ochreous · Jun 12, 2012 at 01:11 AM 0
Share

I still have no clue how to setup an X$$anonymous$$L serialization. Is there a tutorial for how to set one up?

avatar image Berenger · Jun 12, 2012 at 04:07 PM 0
Share

$$anonymous$$aybe this one ? I'm just using google, feel free to do so ;)

avatar image Ochreous · Jun 12, 2012 at 06:03 PM 0
Share

Ok I think I understand how it works now. but I would like to try out JSON too. How do you set it up for JSON? Also which version of JSON C# should I get?

Show more comments
avatar image
0
Wiki

Answer by sooncat · Jun 12, 2012 at 08:21 AM

Save a XML:(UnityEditor only)(PC standalone Mode only)

 using System.Xml;
 public void SaveXML()
 {
   XmlDocument xmlDoc = new XmlDocument();
   //may add Declaration
   //may add root
   XmlElement root = xmlDoc.CreateElement("", "root", "");
   xmlDoc.AppendChild(root);
   //add your data
   XmlElement xmlData = xmlDoc.CreateElement("data");
   xmlData.SetAttribute("myInt", intValue);
   xmlData.SetAttribute("myBool", intValue);
   xmlData.SetAttribute("myFlot", floatValue);
   root.AddpendChild(xmlData);
   //save
   xmlDoc.Save(yourFilePath);
 }


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

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

6 People are following this question.

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

Related Questions

How do you save a player's prefered resolution in Unity 2017.1.1f? 0 Answers

Saving and Loading Various Things 1 Answer

Save/Load Animation State of Instantiated Prefabs 0 Answers

Save and Load Prefab and Mesh 1 Answer

webgl saving. 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