【发布时间】: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;
}
}
【问题讨论】:
-
让 List 是静态的,并且在一个不继承自 monobehaviour 的类中