简体   繁体   中英

Game stops when button is clicked to serialize data and change scene, but no error log from Unity

As you can see in the title, When I run my game in the Unity editor, I click a button after entering a username into an input field that serializes the name and loads the next scene. The problem is that when I click the button, the game freezes and Unity stop responding, but when I remove the code that serializes the name, it loads the next level no problem. I have attached the code that gets the name from the input field, the code that serializes the name and the code that loads the next scene in that order. The problem is most likely in the serializing code, but I'm not entirely sure what the problem is. I edited the code and replaced the code below as Yuri Galiza requested. To see the problem first hand and to get some better context as to what the code is doing, please use this link: https://drive.google.com/drive/folders/0BynpT6Viy1xdR0IwWmV6T3RRU2s?usp=sharing

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine.UI;
using UnityEngine;

[System.Serializable]
public class getUserName : MonoBehaviour
{

public InputField mainInputField;
public string userName;
public static getUserName current;

public void SetName()
{
    userName = mainInputField.text;

    Debug.Log(userName);

    GameObject NameSaver = GameObject.Find("NameSaver");
    SaveName saveName = NameSaver.GetComponent<SaveName>();
    SaveName.nameSave();
}
}


using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine.UI;
using UnityEngine;

public class SaveName : MonoBehaviour
{

public static string PlayerName;

public static List<getUserName> names = new List<getUserName>();

public static void nameSave()
{

    Debug.Log("Saving");

    GameObject InputField = GameObject.Find("InputField");
    getUserName GetUserName = InputField.GetComponent<getUserName>();
    GetUserName.SetName();
    PlayerName = GetUserName.userName;

    Debug.Log("PlayerName");

    names.Add(getUserName.current);
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Create(Application.persistentDataPath + "/savedGames.torlon");
    bf.Serialize(file, SaveName.names);
    file.Close();

    Debug.Log("Saved");
}

public static void nameLoad()
{

    Debug.Log("Loading");

    if (File.Exists(Application.persistentDataPath + "/savedGames.torlon"))
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Open(Application.persistentDataPath + "/savedGames.torlon", FileMode.Open);
        SaveName.names = (List<getUserName>)bf.Deserialize(file);
        file.Close();
    }
    else
    {
        Debug.Log("Load failed");
    }
}
}


using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ButtonManager : MonoBehaviour {

    public int NextScene;

public void OnClick()
    {
        Debug.Log("Loading level");
        SceneManager.LoadScene(NextScene);
    }
}

Well, probably the application is dying before the Log is called. Try placing logs at different places throughout the code. And then you'll probably find the place with the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM