Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Desirialization not working ,

Discussion in 'Scripting' started by cstlmode, Jun 24, 2016.

  1. cstlmode

    cstlmode

    Joined:
    Dec 2, 2014
    Posts:
    88
    hello , i've been trying to figure out what is the issue in this code , with no luck , i was wondering if you guys can help me with this , it gives me this error when i trigger open(); , save() is okey , i guess

    NullReferenceException: Object reference not set to an instance of an object TestDB.Open () (at Assets/TestDB.js:45)

    this line of the function open Health =MyData.SavedHealth ;

    Code (JavaScript):
    1.  import UnityEngine;
    2. import System.Collections;
    3. import System ;
    4. import System.Runtime.Serialization.Formatters.Binary ;
    5. import System.IO;
    6.     class TestDB extends  MonoBehaviour {
    7.         public static var DB :TestDB ;
    8.            public var Health :float ;
    9.            public var Experience :float;
    10.            public var Directory :String;
    11.         function Awake () {
    12.             DB=this;
    13.         }
    14.             function Save ()
    15.             {
    16.                 var BF :BinaryFormatter = new BinaryFormatter();
    17.                 var MySaveFile :FileStream =File.Create( Application.streamingAssetsPath+"/Data.dat" );
    18.                 var MyData :Data = new Data();
    19.                 MyData.SavedHealth =Health;
    20.                 MyData.SavedExperience =Experience;
    21.                 BF.Serialize(MySaveFile ,Data) ;
    22.                 MySaveFile.Close();
    23.      
    24.      
    25.             }
    26.             function Open () {
    27.                 if(File.Exists( Application.streamingAssetsPath+"/Data.dat") )
    28.                 {
    29.                     var BF :BinaryFormatter = new BinaryFormatter();
    30.                     var MySaveFile :FileStream =File.Open(  Application.streamingAssetsPath+"/Data.dat" ,FileMode.Open );
    31.      
    32.                     var MyData :Data = BF.Deserialize(MySaveFile)as Data ;
    33.      
    34.                     MySaveFile.Close();
    35.                     Health =MyData.SavedHealth ;
    36.                     Experience=MyData.SavedExperience ;
    37.                 }
    38.             }
    39.         }
    40.         class Data
    41.         {
    42.            var SavedHealth :float ;
    43.             var SavedExperience :float;
    44.          }
    45.            
     
    Last edited: Jun 24, 2016
  2. cstlmode

    cstlmode

    Joined:
    Dec 2, 2014
    Posts:
    88
    any help , please i'm stuck ,
    i understand that the variables that get deserialized are showing null ,MyData.SavedHealth , MyData.SavedExperience , but why is that , i don't see a problem in the serialization or deserialisation process, if someone has an idea , please help

    Code (JavaScript):
    1.   function Open () {
    2.                 if(File.Exists( Application.streamingAssetsPath+"/Data.dat") )
    3.                 {
    4.                     var BF :BinaryFormatter = new BinaryFormatter();
    5.                     var MySaveFile :FileStream =File.Open(  Application.streamingAssetsPath+"/Data.dat" ,FileMode.Open );
    6.    
    7.                     var MyData :Data = BF.Deserialize(MySaveFile)as Data ;
    8.      
    9.                     MySaveFile.Close();
    10.                     Health =MyData.SavedHealth ;
    11.                     Experience=MyData.SavedExperience ;
    12.                 }
    13.             }
    14.         }
     
  3. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    The error sounds more like the null object is your TestDB not your data, because float can't be null. I'm really not a fan of how you set the instance there, your class doesn't have anything to do with mono behavior (far as I can see from the script up there), so just make it a simple singleton, here's a c# code of singleton that you can use in unity:
    Code (CSharp):
    1. class Singleton{
    2.   public static Singleton Instance {get; private set;}
    3.   static Singleton{
    4.     Instance = new Singleton();
    5.   }
    6.   private Singleton(){
    7.     //add some initialization here if you want.
    8.   }
    9.   public void Save(){
    10.     //save data here.
    11.   }
    12.   public void Load(){
    13.     //load data here.
    14.   }
    15. }
    16. //when you call from other scripts:
    17. Singleton.Instance.Save();
    just because your script will be used in the game means that it has to inherit MonoBehavior.
    Also have you considered trying to save your data in raw text format just for the sake of checking if it's really working as intended??
     
  4. cstlmode

    cstlmode

    Joined:
    Dec 2, 2014
    Posts:
    88
    Hello Severos ,
    i tried using singelton but it didnt work , so that was not the issue causing this i guess ,
    i tried to simplfy everything , i converted the binaryformatter serializer to Xml serializer and it works fine now , but the same code with binaryformatter does not work , i'm sure i'm just missing something , hope someone can help figure this out ,
    problem is with this line "Health =MyData.SavedHealth ;" in the open function , binaryformatter serializer example


    here is the Xml serializer
    Code (JavaScript):
    1. import UnityEngine;
    2. import System.Collections;
    3. import System.Collections.Generic ;
    4. import System.Xml.Serialization ;
    5. import System.Xml;
    6. import System.IO;
    7.  
    8.  
    9.  
    10.  
    11. public   var MyData :Data ;
    12. public var Health :float ;
    13.  
    14.  
    15. public class TestDB extends  MonoBehaviour {
    16.  
    17.     // save function
    18.     public function Save(){
    19.        
    20.         var MySerializer :XmlSerializer = new XmlSerializer(typeof(Data) );
    21.         MyData.SavedHealth =Health;
    22.         var MySaveFile :FileStream = new FileStream(  Application.streamingAssetsPath+"/Data.xml",FileMode.OpenOrCreate);
    23.         MySerializer.Serialize(MySaveFile,MyData);
    24.  
    25.  
    26.         MySaveFile.Close();
    27.        
    28.     }
    29.         // Open function
    30.     public function Open()
    31.     {
    32.         Debug.Log("MyData.SavedHealth"+MyData.SavedHealth);
    33.         var MySerializer :XmlSerializer= new XmlSerializer(typeof(Data));
    34.  
    35.      
    36.         var MySaveFile :FileStream = new FileStream(Application.streamingAssetsPath+"/Data.xml" ,FileMode.Open);
    37.        
    38.         MyData= MySerializer.Deserialize(MySaveFile) as Data;
    39.         MySaveFile.Close();
    40.       Health =MyData.SavedHealth ;
    41.  
    42.      
    43.     }}
    44.  
    45.  
    46.     public class Data  {
    47.  
    48.           public var SavedHealth :float ;
    49.    
    50.     }
    here is the binaryformatter serializer


    Code (JavaScript):
    1.  
    2. import UnityEngine;
    3. import System.Collections;
    4. import System ;
    5. import System.Runtime.Serialization.Formatters.Binary ;
    6. import System.IO;
    7.  
    8.  
    9. public   var MyData :Data ;
    10. public var Health :float ;
    11.  
    12.  public class TestDB extends  MonoBehaviour {
    13.  
    14.  
    15.  
    16.         public function Save ()
    17.        {
    18.            var BF :BinaryFormatter = new BinaryFormatter();
    19.            var MySaveFile :FileStream =File.Create( Application.streamingAssetsPath+"/Data.dat" );
    20.            MyData.SavedHealth =Health;
    21.        
    22.  
    23.            BF.Serialize(MySaveFile ,Data) ;
    24.            MySaveFile.Close();
    25.    
    26.    
    27.        }
    28.    public function Open () {
    29.          
    30.                var BF :BinaryFormatter = new BinaryFormatter();
    31.                var MySaveFile :FileStream =File.Open(  Application.streamingAssetsPath+"/Data.dat" ,FileMode.Open );
    32.    
    33.                 MyData  = BF.Deserialize(MySaveFile)as Data ;
    34.    
    35.                MySaveFile.Close();
    36.                Health =MyData.SavedHealth ;
    37.            
    38.          
    39.        }
    40.    }
    41.    public  class Data
    42.    {
    43.        public var SavedHealth :float ;
    44.  
    45.        }
    46.  
    47.    
     
    Last edited: Jun 25, 2016
  5. cstlmode

    cstlmode

    Joined:
    Dec 2, 2014
    Posts:
    88
    Hello , anyone ?
     
  6. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    Try changing this line:

    Code (CSharp):
    1.                 BF.Serialize(MySaveFile ,Data) ;
    to this:

    Code (CSharp):
    1. BF.Serialize(MySaveFile, MyData);
     
  7. cstlmode

    cstlmode

    Joined:
    Dec 2, 2014
    Posts:
    88
    @boolfone you sir did it thank you so much :D