我在“团结”中创建了一个简单的2d纸牌游戏,当用户玩游戏时,某些信息正在被捕获,比如(得分、尝试、时间),当用户在游戏中赢或输时,相应的场景(游戏结束或获胜场景)被触发,在这两个场景中,我想将游戏的信息从分数、尝试、时间域复制到gameOver或Win的适当标签上,但似乎我做错了什么。
我试图在StackOverflow、Youtube和网络上找到类似的问题;我查看了文档,并尝试了不同的方法。
这是我目前正在处理的代码,在我看来这是完全正确的,我没有任何错误,只是标签上没有出现任何东西。
'''
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class win_results : MonoBehaviour
{
// public options script;
public Text score_final_text;
public Text tries_final_text;
public Text time_final_text;
public void player_win_results()
{
// script = a.getComponent<options>();
if (options.user_lvl_choice=="easy")
{
GameObject tries_final = GameObject.Find("tries_text_lvl1"); //finds the object named score_results
tries_final_text.text = tries_final.GetComponent<Text>().text + "a"; //gets the Text property of it.
GameObject time_final = GameObject.Find("TimeCounter_lvl1"); //finds the object named score_results
time_final_text.text = time_final.GetComponent<Text>().text + "a"; //gets the Text property of it.
GameObject score_final = GameObject.Find("score_lvl1"); //finds the object named score_results
score_final_text.text = score_final.GetComponent<Text>().text + "a"; //gets the Text property of it.
}
else if (options.user_lvl_choice == "hard")
{
GameObject tries_final = GameObject.Find("tries_text_lvl2"); //finds the object named score_results
tries_final_text.text = tries_final.GetComponent<Text>().text + "a"; //gets the Text property of it.
GameObject time_final = GameObject.Find("TimeCounter_lvl2"); //finds the object named score_results
time_final_text.text = time_final.GetComponent<Text>().text + "a"; //gets the Text property of it.
GameObject score_final = GameObject.Find("score_lvl2"); //finds the object named score_results
score_final_text.text = score_final.GetComponent<Text>().text + "a"; //gets the Text property of it.
}
}正如上面的代码所示,我使用GameObject.Find作为文档状态,我已经双重检查了名称和输入,脚本被正确附加,输出中没有错误,只是什么都没有发生。
正如您在上面看到的,为了调试目的,我在末尾添加了一个额外的"a“字符,"a”字符也没有出现在输出中,这意味着gameObject查找在我的示例中不能正常工作。
我做错了什么?
提前谢谢。
发布于 2019-06-01 10:50:47
加载没有第二个参数的场景时
SceneManager.LoadScene("scene2");旧场景将被销毁,该场景中的所有GameObjects也将被销毁,除非您已经在其上调用了DontDestroyOnLoad。
因此,如果你还没有完成这项工作,你在新的场景中得到的是空洞的数据。您可以打印要检查的值。
通过场景传递数据的最简单方法是使用静态字段。创建一个类并添加一些静态字段:
class Global
{
public static string user_lvl_choice;
public static string tries_final;
....
}然后您可以从任何场景中设置或获取这些值。
Global.user_lvl_choice = "easy"
print(Global.user_lvl_choice);阅读更多信息:
https://stackoverflow.com/questions/56405716
复制相似问题