0
\$\begingroup\$

This is the problematic line

datosAguardar datos = new datosAguardar ();

Here is my code

public class estadoJuego : MonoBehaviour {

public int puntuacionMaxima = 0;
public static estadoJuego estadojuego; 
private string rutaArchivo; 

void Awake ()
{
    rutaArchivo = Application.persistentDataPath + "/datos.dat";
    if (estadojuego == null) 
    {
        estadojuego = this;
        DontDestroyOnLoad (gameObject);

    }  else if (estadojuego != this) 
    {
        Destroy (gameObject);
    }
}

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

// Update is called once per frame
void Update () {

}

public void guardar ()
{
    BinaryFormatter bf = new BinaryFormatter ();
    FileStream file = File.Create (rutaArchivo);

    datosAguardar datos = new datosAguardar ();

    datos.puntuacionMaxima = puntuacionMaxima; 

    bf.Serialize (file, datos);

    file.Close ();
}


void cargar () 
{
    if (File.Exists (rutaArchivo)) 
    {
        BinaryFormatter bf = new BinaryFormatter ();
        FileStream file = File.Open (rutaArchivo,FileMode.Open);

        datosAguardar datos = (datosAguardar)bf.Deserialize (file);

        puntuacionMaxima = datos.puntuacionMaxima; 

        file.Close ();
    }  else 
    {
        puntuacionMaxima = 0; 
    }
}


[Serializable]
//definiendo la clase
class datosAguardar  //: ISerializable 

{
    //propiedades de la clase
    public int puntuacionMaxima ; 
    //metodo de la clase
    //constructor de la clase
    public datosAguardar (int puntuacionMaxima) // : base (puntuacionMaxima)
    {       

        this.puntuacionMaxima = puntuacionMaxima  ;
    }

}

This is the constructor

[Serializable]
//definiendo la clase
class datosAguardar  //: ISerializable 

{
    //propiedades de la clase
    public int puntuacionMaxima ; 
    //metodo de la clase
    //constructor de la clase
    public datosAguardar (int puntuacionMaxima) // : base (puntuacionMaxima)
    {       

        this.puntuacionMaxima = puntuacionMaxima  ;
    }

}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ What is the error that Unity is giving you? \$\endgroup\$ Sep 25 '16 at 1:06
  • 1
    \$\begingroup\$ Voting to close as off topic. This question is not specific to game development and thus should be asked on stack overflow. \$\endgroup\$
    – Vaillancourt
    Sep 25 '16 at 2:25
1
\$\begingroup\$

The datosAguardar class only has a contructor that takes 1 argument:

public datosAguardar (int puntuacionMaxima)

so you either need to supply that argument, e.g.

datosAguardar datos = new datosAguardar (5);

or define another constructor that takes no arguments, e.g.:

public datosAguardar ()
{       

    this.puntuacionMaxima = 7;
}

or give the parameter in the existing constructor a default value, e.g.:

public datosAguardar (int puntuacionMaxima = 8)
{       

    this.puntuacionMaxima = puntuacionMaxima;
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Please avoid answering off topic questions :) \$\endgroup\$
    – Vaillancourt
    Sep 25 '16 at 2:25
  • \$\begingroup\$ @AlexandreVaillancourt, the time stamps show that you commented on this answer before voting to close. I have to ask if you would have known this was a generic programming problem, if you had not read the answer in the first place. Related meta \$\endgroup\$
    – Gnemlock
    Sep 25 '16 at 3:51
  • \$\begingroup\$ @Gnemlock Do you mean "if you had not read the question in the first place"? \$\endgroup\$
    – Vaillancourt
    Sep 26 '16 at 15:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.