Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to save gamesdata in file but in gives error cannot Convert GameData to System.To.Stream

Severity Code Description Project File Line
Error CS1503 Argument 1: cannot convert from 'GameData' to 'System.IO.Stream' Assembly-CSharp-vs GameControle.cs 43


......... Help Me ........


C#
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary; 


public class GameControler : MonoBehaviour {

    public static GameControler instance;

    private GameData data;

    public int cruntSchore;
    public int cruntLevel;

    public bool IsGameStartedFirstTime;
    public bool IsMusicOn;

    public int HighSchore;

    public bool[] Levels;

    // Use this for initialization
    void Start () {
	
	}


    public void Save() {
        FileStream file = null;
        try {
            BinaryFormatter bf = new BinaryFormatter();
            file = File.Create(Application.persistentDataPath + "/Gamedata.data");
            if(data != null){
                data.setHighSchore(HighSchore);
                data.setLevels(Levels);
                data.SetIsGameStartedFirstTime(IsGameStartedFirstTime);
                data.setIsMusicOn(IsMusicOn);
                bf.Serialize(data, file); //cannot Convert GameData to System.To.Stream 
            }
        } catch(Exception e) { }
        finally {
            file.Close();
        };

    }

[Serializable]
class GameData {
    private bool IsGameStartedFirstTime;
    private bool IsMusicOn;
    
    private int HighSchore;

    private bool[] Levels;

    public void SetIsGameStartedFirstTime(bool IsGameStartedFirstTime)
    {
        this.IsGameStartedFirstTime = IsGameStartedFirstTime;
    }
    public bool GetIsGameStartedFirstTime()
    {
        return IsGameStartedFirstTime;
    }
    public void setIsMusicOn(bool IsMusicOn)
    {
        this.IsMusicOn = IsMusicOn;
    }

    public bool getIsMusicOn()
    {
        return IsMusicOn;
    }

    public void setHighSchore(int HighSchore)
    {
        this.HighSchore = HighSchore;
    }

    public int getHighSchore()
    {
        return HighSchore;
    }

    public void setLevels(bool[] Levels)
    {
        this.Levels = Levels;
    }

    public bool[] getLevels()
    {
        return Levels;
    }
}
Posted
Updated 17-Aug-15 0:12am
v3

1 solution

Try to change your GameData class and use public properties instead.

C#
[Serializable]
class GameData
{
    public bool IsGameStartedFirstTime { get; set; }

    public bool IsMusicOn { get; set; }

    public int HighSchore { get; set; }

    public bool[] Levels { get; set; }
}


[UPDATE]
Well, I thought you had a runtime error not a compiler error.
This line
C#
bf.Serialize(data, file);

should be
C#
bf.Serialize(file, data);

(Maybe open the help???)

Try this code as a working sample:
C#
public void Save()
{
    try
    {
        GameData data = new GameData();
        data.HighSchore = 1024;
        data.IsGameStartedFirstTime = true;
        data.IsMusicOn = false;
        data.Levels = new bool[10];
        data.Levels[0] = true;
        data.Levels[1] = true;

        using (FileStream file = File.Create(@"C:\Temp\Gamedata.data"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(file, data); // CAN Convert GameData to System.Stream 
        }

        GameData dataFromFile = new GameData();
        using (FileStream file = File.OpenRead(@"C:\Temp\Gamedata.data"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            dataFromFile = (GameData)bf.Deserialize(file);
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }
}
 
Share this answer
 
v2
Comments
sdsoft.comze.com 17-Aug-15 4:52am    
it cannot Convert GameData to System.To.Stream
George Jonsson 17-Aug-15 5:14am    
See updated answer.
Next time be a little bit more specific about the kind of error you get.
If you had said compiler error, I would have spotted the error faster.
sdsoft.comze.com 17-Aug-15 5:23am    
Severity Code Description Project File Line
Error CS1503 Argument 1: cannot convert from 'GameData' to 'System.IO.Stream' Assembly-CSharp-vs GameControle.cs 43
George Jonsson 17-Aug-15 6:02am    
Have you tried the code I provided you?
It works.
sdsoft.comze.com 17-Aug-15 6:15am    
Thanks.. but You Explain why and ware error occurred. tanks again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900