I have been trying to solve the error for hours, I have a script which I use to pass information between scenes, like the character I selected in the character selection scene that I have created. All this works, but the script is disabled when entering the scene, and I have to activate it manually every time I enter the game, which I can not do from a mobile device.
Here the script:
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class conservar : MonoBehaviour
{
public static conservar estado;
private string rutaArchivo;
public int per = 0;
public int puntuacionMaxima = 0;
public int lastpunt = 0;
public Meters M;
public int Mtrs = 0;
void Awake()
{
GameObject CharSel = GameObject.Find("/personaje/CHarSel");
CharSel.GetComponent<CharSel>();
GameObject DispChar = GameObject.Find("/DispChar");
DispChar.GetComponent<DispChar>();
rutaArchivo = Application.persistentDataPath + "/datos.dat";
if (estado==null)
{
estado = this;
DontDestroyOnLoad(gameObject);
}
else if (estado != this)
{
Destroy(gameObject);
}
}
// Use this for initialization
void Start()
{
Cargar();
}
// Update is called once per frame
void Update()
{
per = DispChar.perso;
if (per == 0)
{
CharSel.Character = 0;
}
if (per == 1)
{
CharSel.Character = 1;
}
if (per == 2)
{
CharSel.Character = 2;
}
if (per == 3)
{
CharSel.Character = 3;
}
lastpunt = M.meters;
}
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]
class DatosAGuardar
{
public int puntuacionMaxima;
}
}