Script is disabled when the scene starts - C # Unity2d

1

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;
    }
}
    
asked by rogarmu8 14.11.2016 в 20:21
source

1 answer

1

Answer: After more hours of searching I found the error:

GameObject CharSel = GameObject.Find("/personaje/CHarSel");
CharSel.GetComponent<CharSel>();

The object which I was looking for in this line of code called CharSel, was located in another scene, which did not allow the script to advance since it was looking for this object infinitely, deleting this line of code and adding a reference to the object, adding to the beginning of the code:

public GameObject CharSel;

and dragging the prefab of the CharSel object from the editor solves the error.

    
answered by 15.11.2016 / 19:13
source