简体   繁体   English

单击按钮以序列化数据和更改场景时,游戏停止,但是Unity没有错误日志

[英]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.

相关问题 单击时,重新加载场景按钮在Unity 5.3上不起作用 - Reload Scene Button doesn't work on Unity 5.3 when clicked 单击所有对象时自动更改场景 Unity - Change scene automatically when all objects are clicked Unity 单击一定数量的按钮时如何统一更改场景 - How to change a scene in unity when a certain amount of buttons are clicked 如何在Unity中单击按钮时显示问答游戏问题? - How to get quiz game questions to show when button is clicked in Unity? 在Unity中单击时如何更改游戏对象的颜色? - How to Change Color of Game Object When Clicked in Unity? 更改场景时按返回按钮android关闭游戏 - press back button android when change scene close the game 在场景Unity中未检测到输入。 从该场景开始游戏时有效; 否则不起作用 - Input not being detected in scene Unity. Works when starting the game from that scene; does not work otherwise 从Firebase获取数据后尝试更改场景时Unity脚本停止工作 - Unity Script stop working when trying to change scene after get data from firebase 单击按钮会停止一会儿 - It stops for a while when button is clicked Photon/Unity - 尝试退出游戏场景进入菜单场景时遇到错误 - Photon/Unity - Attempting to exit the game scene into the menu scene hits an error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM