[英]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. 如标题所示,当我在Unity编辑器中运行游戏时,在将用户名输入序列名并输入下一个场景的输入字段中输入名称后,单击按钮。 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. 问题是,当我单击该按钮时,游戏将冻结并且Unity停止响应,但是当我删除序列化名称的代码时,它将加载下一个级别,没有问题。 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. 我按照Yuri Galiza的要求编辑了代码并替换了下面的代码。 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 要直接查看问题并获得有关代码功能的更好的上下文,请使用此链接: 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. 好吧,可能是应用程序在调用Log之前就快死了。 Try placing logs at different places throughout the code. 尝试在整个代码中的不同位置放置日志。 And then you'll probably find the place with the problem. 然后您可能会发现问题所在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.