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 fledi1 · Jan 03, 2016 at 09:12 AM · c#androidunity 5serializationfilestream

C# Serialization is working fine in Unity but not on Android

I'm aware, that there are multiple Questions about a similar topic out there already but still wasn't able to find a solution. I keep having the same problem, as already mentioned in the title I try saving data on android and in Unity everything is working as expected but as soon as i try on a real device it isn't saving anything (I tried 4 different devices). So today I tried again and even copied the code from an official tutorial (https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading) but I stil couldn't get it working. Here is my Script:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 using System;

 public class ScoreControll : MonoBehaviour {

 public int Score = 0;
 
 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     GameObject.Find("Score").GetComponent<Text>().text = "Score: " + Score;
 }
 void OnDisable()
 {
     Save();
 }
 void OnEnable()
 {
     Load();
 }
 public void Save()
 {
     BinaryFormatter bf = new BinaryFormatter();
     FileStream file = File.Create(Application.persistentDataPath + "/score.dat");

     ScoreData data = new ScoreData();
     data.Score = Score;

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

     }
 }
 }
 [Serializable]
 class ScoreData
 {
     public int Score;
 }

I really hope for an easy answer.

Thanks in advance,

Pit Hüne

P.S. Im using Unity version 5.3.0f4 but I had the same issue in former versions aswell.

Comment
Add comment · Show 1
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 damand · Jun 28, 2016 at 03:27 PM 0
Share

Hey,

I have the same issue. I am trying to create .dat file for saving username to external storage, but for some reason i could not get the file written to the location (app_domain/data/files). Thanks in advance

Daman

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by fledi1 · Jan 10, 2016 at 07:24 AM

I finally figured out the problem myself it was not a problem with saving, but with the methods. onDestroy , onDisable and similar ones, they are just not beeing called on android.

Comment
Add comment · Show 1 · 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 t-schulz · Feb 03, 2016 at 03:20 PM 0
Share

They are never called on android? So there is no callback on quit/kill the unity android app?

avatar image
0

Answer by Yury-Habets · Jan 04, 2016 at 08:36 AM

What is the error you are getting?

Can you consider using PlayersPrefs http://docs.unity3d.com/ScriptReference/PlayerPrefs.html for saving data?

Comment
Add comment · Show 1 · 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 fledi1 · Jan 04, 2016 at 01:41 PM 0
Share

Thanks for your fast Answer, but I dont get any errors, thats why I think im doing something really basic wrong. I just tried the same thing with PlayerPrefs and again its all working fine in the emulator but as soon as I install and run it on a real android device it's not working anymore.

$$anonymous$$ Hüne

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

8 People are following this question.

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

Related Questions

Unity WWW gzip 1 Answer

Unity Multiplayer Android Game 0 Answers

How can I read an XML file in Android ? I can't access it 1 Answer

Draw a line from object to indicate power 0 Answers

Multiple Cars not working 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