C# Unity(C):如何从不同的场景调用列表?
Unity C:我如何从不同的场景中调用列表?我在当前场景中得到nullReferenceException bc,它没有设置任何变量。但是在前一个场景中,它有我想要的正确的值和变量。注意:这两个场景是不同的,但我想使用分数作为场景之间的共享值 否则,除了检索框架文本[9]。文本位置,您还有其他方法吗C# Unity(C):如何从不同的场景调用列表?,c#,list,unity3d,scene,C#,List,Unity3d,Scene,Unity C:我如何从不同的场景中调用列表?我在当前场景中得到nullReferenceException bc,它没有设置任何变量。但是在前一个场景中,它有我想要的正确的值和变量。注意:这两个场景是不同的,但我想使用分数作为场景之间的共享值 否则,除了检索框架文本[9]。文本位置,您还有其他方法吗 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.U
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;
}
}
在包含从一个场景到另一个场景的变量的对象的脚本中使用。您可以使用DontDestroyOnLoadtransform.gameObject;以便游戏对象在下一场景中处于活动状态,或使用PlayerPrefs保存数据并加载到下一场景中。两者都会起作用 我认为你的要求取决于你想在项目中做什么。 但我可以推荐一些解决方案: 独生子女- 将分数保存在外部文件中 PlayerPref
如果你想保持整场比赛的成绩,我想单身汉会很好 让列表是静态的,并且位于一个类中,该类不继承自我使用的DontDestroyOnLoad作为dontDestroyOnLoadFrameText[I];,的可能重复项;。但是,我得到警告DontDesotryOnLoad仅适用于根游戏对象或根游戏对象上的组件。NullReferenceException会像以前一样发生。有没有线索说明它为什么不工作?不能在变量上使用DontDestroyOnLoad。将脚本附加到一个空的游戏对象,并将其称为DontDestroyOnLoadtransform.gameobject;transform.gameObject是您不会销毁的对象。请注意,您只需在MonoBehavior类上使用DontDestroyOnLoadgameObject或DontDestroyOnLoadthis,甚至不确定Unity示例为什么是DontDestroyOnLoadtransform.gameObject:这会创建无用的调用。还要记住,持有DontDestroyOnLoad脚本的对象必须是根对象,而不是另一个对象的子对象:如果父对象没有DontDestroyOnLoad脚本,则其子对象将被删除。