Search Unity

Loading a .dat file using FileStream error

Discussion in 'Scripting' started by Josh1994, Mar 30, 2016.

  1. Josh1994

    Josh1994

    Joined:
    Mar 26, 2016
    Posts:
    21
    I am trying to load my playerData.dat file and display a float variable to a textbox in Unity, however, I am getting an error on line 27.
    Code (csharp):
    1.  
    2. // Code hidden as problem now solved
    3.  
    Line 27 is coming up with an error saying "InvalidCastException: Cannot cast from source type to destination type." Does anyone know why this is?
     
    Last edited: Apr 25, 2016
  2. Josh1994

    Josh1994

    Joined:
    Mar 26, 2016
    Posts:
    21
    Anyone have any ideas? I'm at a complete loss here
     
  3. Trexug

    Trexug

    Joined:
    Dec 2, 2013
    Posts:
    88
    If you replace line 27 with
    Code (csharp):
    1. Debug.Log(bf.Deserialize(file).GetType().ToString());
    What kind of output do you then get?
     
  4. Josh1994

    Josh1994

    Joined:
    Mar 26, 2016
    Posts:
    21
    My output is 'PlayerData', followed by a NullReferenceException saying 'Object reference not set to an instance of an object'
     
  5. RuanJacobz

    RuanJacobz

    Joined:
    Jan 24, 2014
    Posts:
    59
    Mate I'm getting the idea that the problem might be that your save method isn't compatible with your load method. Could you post your save method to see if we can spot something off there?
     
  6. Josh1994

    Josh1994

    Joined:
    Mar 26, 2016
    Posts:
    21
    I seem to have found the problem lyed with this bit of code, so I commented it out and it now works as it should
    Code (csharp):
    1.  
    2. [Serializable]
    3. class PlayerData
    4. {
    5.   public float rightTotalDisplacement;
    6.   public float leftTotalDisplacement;
    7. }
    8.  
    My Save and Load method works fine on another C# script I had, I just didn't realise this bit of code wasn't needed when you're not saving