Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Chellebell1689 · Jan 20, 2018 at 10:10 PM · save data

Saving Data for a Class

Hello,

I'm creating an app for demos and I'm still fairly new to programming. I have a class that will have all my demo information and I am using ".GetComponent" to get the text entered into the input field to assign it to the appropriate information (really hoping this is right). What I need help with is how to save this class information to a file and then read the information later (will also need to rewrite when necessary).

Can someone point me to a good tutorial for saving a class to a file & reading it again later? TIA!

P.s. Let me know if you need more information.

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
1

Answer by LINKENN · Jan 20, 2018 at 11:00 PM

Hello, I am not sure what are you looking for namely, but I suppose something like this. When you character travels between different scenes, you can you use maybe this method. If you character travels from one scene to another then all information is going to save in one file together with changes and than you can load all information in another scene.

https://unity3d.com/ru/learn/tutorials/topics/scripting/persistence-saving-and-loading-data Persistence - Saving and Loading Data

I send 2 example scripts

first allows you to save and load data. There were you will see a bla stuff, you shoud need to add all you parametrs


// https://unity3d.com/ru/learn/tutorials/topics/scripting/persistence-saving-and-loading-data Persistence - Saving and Loading Data

// script works + public class AdjustScript : MonoBehaviour using System.Collections; using UnityEngine; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO;

public class GameControl : MonoBehaviour { public static GameControl control;

 public float health;
 public float experience;

 public float bla;  /// you need to add here all what you need

 void Awake () // cames for  Stort ()
 {
     if(control == null)
     {
     DontDestroyOnLoad (gameObject);
     control = this;
     }
     else if(control != this)
     {
         Destroy (gameObject);
     }    
 }

 void OnGUI () 
 {
     GUI.Label (new Rect (10, 10, 100, 30), "Health: " + health);
     GUI.Label (new Rect (10, 40, 100, 30), "Experience: " + experience);
 }

 // Update is called once per frame
 public void Save () 
 {
     BinaryFormatter bf = new BinaryFormatter ();
     FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");

     PlayerData data = new PlayerData ();
     data.health = health;
     data.experience = experience;

     data.bla = bla;  /// you need to add here all what you need

     bf.Serialize (file, data);
     file.Close ();
 }

 public void Load()
 {
     if(File.Exists(Application.persistentDataPath + "/ playerInfo.dat"))
     {
         BinaryFormatter bf = new BinaryFormatter ();
         FileStream file = File.Open (Application.persistentDataPath + "/ playerInfo.dat", FileMode.Open);
         PlayerData data = (PlayerData)bf.Deserialize(file);
         file.Close ();

         health = data.health;
         experience = data.experience;

         bla = data.bla; /// you need to add here all what you need
     }
 }


[Serializable] class PlayerData

{ public float health; public float experience; public float bla; /// you need to add here all what you need } }

Comment
Add comment · Show 2 · 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 LINKENN · Jan 20, 2018 at 11:01 PM 0
Share

now comes second script, this second script will allow you to change parameters while you are playing the game by GUI buttons


using System.Collections; using UnityEngine;

public class AdjustScript : $$anonymous$$onoBehaviour { // scrip works +public class GameControl : $$anonymous$$onoBehaviour :) // this buttons you can use for manipulation of paramets void OnGUI () { if(GUI.Button(new Rect (10, 100, 100, 30), "Health UP")) { GameControl.control.health += 10; } if(GUI.Button(new Rect (10, 140, 100, 30), "Health Down")) { GameControl.control.health -= 10; } if(GUI.Button(new Rect (10, 180, 100, 30), "Experience Up")) { GameControl.control.experience += 10; } if(GUI.Button(new Rect (10, 220, 100, 30), "Experience Down")) { GameControl.control.experience -= 10; } if(GUI.Button(new Rect (10, 340, 100, 30), "Bla")) // if(GUI.Button(new Rect ( 1200, 340, 100, 30), "Bla")) { GameControl.control.bla -= 10; /// you need to add here all what you need } if(GUI.Button(new Rect (10, 260, 100, 30), "Save")) { GameControl.control.Save(); } if(GUI.Button(new Rect (10, 300, 100, 30), "Load")) { GameControl.control.Load(); } }

}

avatar image LINKENN · Jan 20, 2018 at 11:02 PM 0
Share

something went wrong with this Unity :) sorry for that

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

74 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Saved data reverts when ending editor session 1 Answer

How do I have an EditorWindow save it's data inbetween opening and closing Unity? C# 5 Answers

Call method after loading scene? 1 Answer

Deserializing data with XML Serializer 0 Answers

Problem when trying to save time 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