C# 从场景1到场景2获取标签文本

C# 从场景1到场景2获取标签文本,c#,unity3d,C#,Unity3d,我在Unity中创建了一个简单的2d纸牌游戏,当用户玩游戏时,某些信息会被捕获,如(分数、尝试、时间),当用户赢或输游戏时,会触发相应的场景(游戏结束或赢场景),在这两个场景中,我想从分数中复制信息,尝试,将游戏的时间域设置为gameOver或Win的适当标签,但似乎我做错了什么 我试图在StackOverflow、Youtube和网络上找到类似的问题;我检查了文档,尝试了不同的方法 这是我目前正在使用的代码,对我来说似乎完全正确,我没有收到任何错误,只是标签上没有显示任何内容 ''' usin

我在Unity中创建了一个简单的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.
        }
    }
“”
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类双赢结果:单一行为
{
//公共选择脚本;
公共文本分数\最终\文本;
公共文本尝试最终文本;
公共文本时间\最终文本;
公开无效玩家赢得结果()
{
//script=a.getComponent();
if(options.user\u lvl\u choice==“easy”)
{
GameObject trytes\u final=GameObject.Find(“trytes\u text\u lvl1”);//查找名为score\u results的对象
trytes_final_text.text=trytes_final.GetComponent().text+“a”//获取它的text属性。
GameObject time_final=GameObject.Find(“TimeCounter_lvl1”);//查找名为score_results的对象
time\u final\u text.text=time\u final.GetComponent().text+“a”;//获取它的text属性。
GameObject score\u final=GameObject.Find(“score\u lvl1”);//查找名为score\u results的对象
score\u final\u text.text=score\u final.GetComponent().text+“a”//获取它的text属性。
}
else if(options.user\u lvl\u choice==“硬”)
{
GameObject trytes\u final=GameObject.Find(“trytes\u text\u lvl2”);//查找名为score\u results的对象
trytes_final_text.text=trytes_final.GetComponent().text+“a”//获取它的text属性。
GameObject time_final=GameObject.Find(“TimeCounter_lvl2”);//查找名为score_results的对象
time\u final\u text.text=time\u final.GetComponent().text+“a”;//获取它的text属性。
GameObject score\u final=GameObject.Find(“score\u lvl2”);//查找名为score\u results的对象
score\u final\u text.text=score\u final.GetComponent().text+“a”//获取它的text属性。
}
}
正如上面的代码所示,我使用的是
GameObject.Find
,正如文档所述,我已经仔细检查了名称和打字错误,脚本已正确附加,输出中没有错误,只是什么都没有发生

正如您在上面所看到的,出于调试目的,我在末尾添加了一个额外的“a”字符,“a”字符也不会出现在输出中,这意味着在我的示例中,
gameObject
find不能正常工作

我做错了什么


提前感谢。

当您加载没有第二个参数的场景时

SceneManager.LoadScene("scene2");
旧场景将被销毁,该场景中的所有游戏对象也将被销毁,除非您在其中调用了DontDestroyOnLoad

因此,如果您还没有完成这项工作,那么在新场景中得到的是空数据。您可以打印要检查的值

通过场景传递数据的最简单方法是使用静态字段。创建一个类并添加一些静态字段:

class Global
{
     public static string user_lvl_choice;
     public static string tries_final;
     ....
}
然后可以从任何场景设置或获取这些值

Global.user_lvl_choice = "easy"
print(Global.user_lvl_choice);
阅读更多:


兄弟,谢谢你的时间和努力。大拇指朝上因为文本是一个游戏对象,我把3个文本文件放在dontDestroyOnLoad上,但我仍然没有得到输出,仍然在调查如果你不知道如何使用dontDestroyOnLoad,阅读文档或进行一些研究,用几句话就不容易解释了。你可以保存字符串值(不是文本对象),然后将它们设置为新场景中的文本对象。我使用了静态字段,一切都很好,mate,我也了解dontdestroyonload的工作原理。谢谢mate。