Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Persistent Data Storage - Unity Serialization Error on iOS

Discussion in 'iOS and tvOS' started by Hasben, Nov 3, 2014.

  1. Hasben

    Hasben

    Joined:
    Aug 25, 2014
    Posts:
    2
    Error in Xcode:

    SerializationException: Unexpected binary element: 255

    at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject (BinaryElement element, System.IO.BinaryReader reader, System.Int64& objectId, System.Object& value, System.Runtime.Serialization.SerializationInfo& info) [0x00000] in <filename unknown>:0

    at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject (BinaryElement element, System.IO.BinaryReader reader) [0x00000] in <filename unknown>:0

    at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectGraph (BinaryElement elem, System.IO.BinaryReader reader, Boolean readHeaders, System.Object& result, System.Runtime.Remoting.Messaging.Header[]& headers) [0x00000] in <filename unknown>:0

    at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) [0x00000] in <filename unknown>:0

    at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) [0x00000] in <filename unknown>:0

    at GameControl.Load () [0x00000] in <filename unknown>:0

    at OnClickNavigateTo.Start () [0x00000] in <filename unknown>:0


    (Filename: Line: -1)





    Serialization Class in Unity (GameControl.cs):
    ///////BEGIN GameControl.cs CODE BLOCK
    usingUnityEngine;
    usingSystem.Collections;
    usingSystem.Collections.Generic;
    usingSystem;
    usingSystem.Runtime.Serialization.Formatters.Binary;
    usingSystem.IO;

    //PersistentDataStorage
    public class GameControl : MonoBehaviour {

    public static Game Controlcontrol;
    public bool DebugMode = false;
    public string locationString = "/saveData.dat";
    public int playthroughs;
    public int currentCharacter;

    void Awake (){
    if(control == null) {
    DontDestroyOnLoad(gameObject);
    control = this; ​
    } else if(control != this) {
    Destroy(gameObject); ​
    } ​
    }

    void OnGUI(){
    if (DebugMode){
    GUI.Label (new Rect (10, 10, 100, 30), "App Usage: " + playthroughs);
    GUI.Label (new Rect (10, 40, 150, 30), "Character Target: " + currentCharacter); } ​
    }​

    public void Save(){
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Create(Application.persistentDataPath + locationString);​

    PlayerData data = new PlayerData (); data.playthroughs = playthroughs; data.currentCharacter = currentCharacter;​

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

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

    playthroughs = data.playthroughs;
    currentCharacter = data.currentCharacter;​
    }​
    }​
    }

    [Serializable]
    class PlayerData{
    public int playthroughs;
    public int currentCharacter;​
    }

    ///////END GameControl.cs CODE BLOCK




    Everything runs perfectly on my MacBook, but not on iPhone4S which must, for my current needs, be my target platform.
    Ever run into this? Not much help available online.

    Thank you for your help.
    - Ben
     
    Last edited: Nov 3, 2014
  2. Hasben

    Hasben

    Joined:
    Aug 25, 2014
    Posts:
    2
    Solution Found! My good friend Ari found this thread, that had the solution I needed: http://answers.unity3d.com/questions/30930/why-did-my-binaryserialzer-stop-working.html

    Here is the critical excerpt that fit into my class:
     
    TMPxyz, rcashman and New_To_Unity_Guy like this.
  3. New_To_Unity_Guy

    New_To_Unity_Guy

    Joined:
    May 30, 2014
    Posts:
    31
    I was thinking about that post as soon as I saw your post title. Glad you found it. It sure saved me back when I needed it.