Search Unity

Problem with using a List<float> within a class

Discussion in 'Scripting' started by Josh1994, Apr 8, 2016.

  1. Josh1994

    Josh1994

    Joined:
    Mar 26, 2016
    Posts:
    21
    I have a number of lists defined in my class 'PlayerData', however, they are not by my instance of 'PlayerData' through my variable 'data', and as a result, line 6 in my 2nd set of code is coming up with an error saying 'ArgumentException: Object type System.Single cannot be converted to target type: System.Collections.Generic.List`1[System.Single]', does anyone know what I have to change it to in order to get it working?
    Code (csharp):
    1.  
    2. // Code now hidden as problem now solved
    3.  
     
    Last edited: Apr 25, 2016
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    What is the exact stack trace?

    Because line 6 of your code:

    Code (csharp):
    1.  
    2. data=(PlayerData)bf.Deserialize(file);
    3.  
    Has nothing to do with float/single, nor Lists/List<float>.

    So I'm assuming somewhere in the code called by Deserialize something is happening... so posting the full stack trace would be useful.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Well this now is pointing at a different line of code. Line 21 is:

    Code (csharp):
    1.  
    2. data=new PlayerData();
    3.  
    And again, there's nothing there that suggests a float is being assigned to a field that is typed as a List<float>.


    See the exception you're getting means that somewhere in your code there is a variable that is a List, but a float isn't getting added to the list, but instead the variable is being set to a float. And that can't be done.

    I was hoping the full stack trace would point to that line of code, but it still hasn't.

    The exception, as it shows in the 'console' in unity. Click on it, right click, and select 'copy'. If you paste that in here you should get a fairly long thing with the exception and a very detailed stack trace of all methods called. I don't really care about the line numbers...
     
  4. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Your lists are NULL, when you create PlayerData thats fine but you never create a list<float> using the 'new' keyword.
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    That would result in a NullReferenceException not the ArgumentException that's being thrown
     
  6. Josh1994

    Josh1994

    Joined:
    Mar 26, 2016
    Posts:
    21
    Here's the whole thing:

    ArgumentException: Object type System.Single cannot be converted to target type: System.Collections.Generic.List`1[System.Single]
    Parameter name: val
    System.Reflection.MonoField.SetValue (System.Object obj, System.Object val, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoField.cs:133)
    System.Reflection.FieldInfo.SetValue (System.Object obj, System.Object value) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/FieldInfo.cs:150)
    System.Runtime.Serialization.Formatters.Binary.ObjectReader.SetObjectValue (System.Object parentObject, System.String fieldName, System.Reflection.MemberInfo memberInfo, System.Runtime.Serialization.SerializationInfo info, System.Object value, System.Type valueType, System.Int32[] indices) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:799)
    System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadValue (System.IO.BinaryReader reader, System.Object parentObject, Int64 parentObjectId, System.Runtime.Serialization.SerializationInfo info, System.Type valueType, System.String fieldName, System.Reflection.MemberInfo memberInfo, System.Int32[] indices) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:721)
    System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectContent (System.IO.BinaryReader reader, System.Runtime.Serialization.Formatters.Binary.TypeMetadata metadata, Int64 objectId, System.Object& objectInstance, System.Runtime.Serialization.SerializationInfo& info) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:306)
    System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectInstance (System.IO.BinaryReader reader, Boolean isRuntimeObject, Boolean hasTypeInfo, System.Int64& objectId, System.Object& value, System.Runtime.Serialization.SerializationInfo& info) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:270)
    System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject (BinaryElement element, System.IO.BinaryReader reader, System.Int64& objectId, System.Object& value, System.Runtime.Serialization.SerializationInfo& info) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:195)
    System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject (BinaryElement element, System.IO.BinaryReader reader, System.Int64& objectId, System.Object& value, System.Runtime.Serialization.SerializationInfo& info) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:223)
    System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject (BinaryElement element, System.IO.BinaryReader reader) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:130)
    System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectGraph (BinaryElement elem, System.IO.BinaryReader reader, Boolean readHeaders, System.Object& result, System.Runtime.Remoting.Messaging.Header[]& headers) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:104)
    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:179)
    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:136)
    Level1.Load () (at Assets/Scripts/Level1.cs:591)
    Level1.Start () (at Assets/Scripts/Level1.cs:56)
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You're deserialize is barfing. It's trying to assign a field value but the type is different than what it was expecting. Did you change the signature of the PlayerData class and then attempted to load an older version of the .dat file?
     
    lordofduct likes this.
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    So it's the serializer.

    And it's when the serializer tries to set the value for one of those list fields.

    I'm willing to bet that you previously saved a version of: Application.persistentDataPath+"/playerData.dat", when this class had a field that was type 'float', but then you changed the type to List<float>... and now when you try to deserialize it... the data says it's supposed to be a float, but the declared type is expecting a list, and the serializer doesn't know how to deal with that.
     
  9. Josh1994

    Josh1994

    Joined:
    Mar 26, 2016
    Posts:
    21
    Yes you're right I was trying to use a previous .dat when all the fields were floats, so i'm using a new .dat file now and i've managed to get everything working, thanks for everyone's help :)