Hi All,
I’m trying to get a genetic to work with Serialize, but am having no luck. Basically when I read objects to a code it gives an error. “EndOfStreamException: Failed to read past end of stream.”
What I do in a test is
1.open file.
2 write data
3.read data back. Reading causes the issue.
The way I set up the code is in a class that has the following
public void setSavePath( string game_path )
{
this.filepath = Application.dataPath + "/Resources/" + game_path;
}
public void openFile( string game_path )
{
this.setSavePath( game_path );
this.bf = new BinaryFormatter( );
this.file = File.Create( filepath );
this.file.Flush( );
}
public void closeFile( )
{
this.file.Close( );
}
public void writeFile< T >( T testObject )
{
this.bf.Serialize( file, testObject );
this.file.Flush( );
}
public T readFile< T >( )
{
return( ( T )this.bf.Deserialize( file ) );
}
I test the code with the following
file.openFile( "TestAnObject" );
write.firstname = "jim";
write.surname = "henson";
write.age = 65;
file.writeFile< testclass2 >( write );
read = file.readFile< testclass2 >( );
Debug.Log( read.firstname );
Debug.Log( read.surname );
Debug.Log( read.age );
It’s the public T readFile< T >( ) that gives the error. Can anyone advise please?