【问题标题】:Unity (C#): How do I call a List<int> from a different Scene?Unity (C#):如何从不同的场景调用 List<int>?
【发布时间】:2017-06-02 13:37:35
【问题描述】:

Unity (C#):如何从不同的场景调用列表?我在当前场景中得到 nullReferenceException bc,它没有设置任何变量。但在前一个场景中,它具有我想要的正确值和变量。 (注意:两个场景不同,但我想将分数用作场景之间的共享值)。

否则,您是否有替代检索“frameTexts[9].text”位置的方法?

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


public class ScoreDisplay : MonoBehaviour
{

    public Text[] rollTexts, frameTexts;
    static string output;

    public void FillRolls(List<int> rolls)
    {
        string scoreString = FormatRolls(rolls);
        for (int i = 0; i < scoreString.Length; i++)
        {
            rollTexts[i].text = scoreString[i].ToString();

        }
    }

    public void Fillframes(List<int> frames)
    {
        for (int i = 0; i < frames.Count; i++)
        {
            frameTexts[i].text = frames[i].ToString();
            Debug.Log(frameTexts[i].text + " frames");
        }
    }

    public static string FormatRolls(List<int> rolls)
    {
      //  ScoreDisplay scoreDisplay = new ScoreDisplay();
        output = "";

        for (int i = 0; i < rolls.Count; i++)
        {
            int box = output.Length + 1;                            // Score box 1 to 21 

            if (rolls[i] == 0)
            {                                   // Always enter 0 as -
                output += "-";
            }
            else if ((box % 2 == 0 || box == 21) && rolls[i - 1] + rolls[i] == 10)
            {   // SPARE anywhere
                output += "/";
            }
            else if (box >= 19 && rolls[i] == 10)
            {               // STRIKE in frame 10
                output += "X";
            }
            else if (rolls[i] == 10)
            {                           // STRIKE in frame 1-9
                output += "X ";
            }
            else
            {
                output += rolls[i].ToString();                      // Normal 1-9 bowl
            }

        }
        return output;
    }
    public string DisplayFinalScore()
    {
        string FinalScore = "Congrats on scoring: " + frameTexts[9].text;
        return FinalScore;
    }

} 

【问题讨论】:

标签: c# list unity3d scene


【解决方案1】:

在脚本中使用DontDestroyOnLoad 用于包含要从一个场景保留到另一个场景的变量的对象。

【讨论】:

  • 我将 DontDestroyOnLoad 用作“DontDestroyOnLoad(frameTexts[i]);”。但是,我收到警告“DontDesotryOnLoad 仅适用于根 GameObjects 或根 GameObject 上的组件”。 NullReferenceException 像以前一样发生。任何线索为什么它不工作?
  • 您不能对变量使用 DontDestroyOnLoad。将您的脚本附加到一个空的游戏对象并调用此 DontDestroyOnLoad(transform.gameObject); transform.gameObject 是你不会销毁的对象。
  • 请注意,您可以在 MonoBehaviour 类上简单地使用 DontDestroyOnLoad(gameObject) 或 DontDestroyOnLoad(this)(甚至不确定为什么 Unity 示例是 DontDestroyOnLoad(transform.gameObject):这会产生无用的调用)。还要记住你的对象持有DontDestroyOnLoad 脚本必须是一个根对象(不是另一个对象的子对象):如果父对象没有DontDestroyOnLoad 脚本,它的子对象将被删除。
【解决方案2】:

我认为您的要求取决于您想在项目中做什么。 但我可以推荐一些解决方案:

  • 单身人士 (unitygeek - gist)
  • 将您的分数保存在外部文件中
  • PlayerPref

如果你想在整个游戏中保持分数,我认为singleton会很好!

【讨论】:

    【解决方案3】:

    您可以使用DontDestroyOnLoad(transform.gameObject); 使游戏对象在您的下一个场景中处于活动状态,或者使用PlayerPrefs 保存数据并在下一个场景中加载。两者都可以!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多