Unity(C#):如何调用List< int>来自不同的场景? [英] Unity (C#): How do I call a List<int> from a different Scene?

查看:428
本文介绍了Unity(C#):如何调用List< int>来自不同的场景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Unity (C#): How do I call a List from a different Scene? I get nullReferenceException bc in the current scene, it has no variables set to it. But in the previous Scene it had the correct values and variables that I wanted. (note: the 2 scenes are different but I want to use the score as a shared value between Scenes).

否则,除了检索"frameTexts [9] .text"位置之外,您还有其他选择吗?

Otherwise, do you have an alternative to retrieving the "frameTexts[9].text" position?

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;
    }

} 

推荐答案

我认为您的请求取决于您要在项目中执行的操作. 但我可以推荐一些解决方案:

I think your request depend on what you wanna do in your project. But i can recommend some few solutions:

  • Singleton (unitygeek - gist)
  • Save your scores in external file
  • PlayerPref

如果您想在整场比赛中保持得分,我认为单人会很好!

If you wanna keep the scores entire the whole game, i think singleton will be nice !!

这篇关于Unity(C#):如何调用List&lt; int&gt;来自不同的场景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆