所以你需要确保玩家按正确的顺序点击颜色?
首先,您的代码存在问题。 Colorize 是动词,只能用于函数和方法名,这就是为什么 StackOverflow 给它一个颜色,它认为它是一个函数。名字听起来是Colors。 (我知道,“s”在英语中听起来像“z”很奇怪,但有一个有趣的原因)。
首先,像这样创建一个enum:
public class BallUtilities
{
enum BallColors { Red, Blue, Green, Yellow, Pink, Purple ... };
public static Color32 ToColor(this BallColor ballColor) =>
{
switch (ballColor)
{
case BallColor.Red: return new Color32 (255, 50, 50);
case BallColor.Blue: return new Color32 (50, 50, 255);
case BallColor.Green: ...
.
.
.
(etc.)
}
}
}
现在,我们需要跟踪所有创建的球,所以:
public class BallManager
{
public static List<BallColor> BallColorsSpawned = new List<BallColor>();
}
然后,将您当前的脚本更改为:
public void Start()
{
Color = (BallColor)Random.Range(0, Enum.GetNames(typeof(BallColor)).Length - 1)
GetComponent<SpriteRenderer>().Color = Color.ToColor();
Ballmanager.SpawnBallsColor.Add(Color);
}
现在我们按照玩家需要击中它们的顺序列出了所有颜色。所以现在,无论玩家在哪里击球,都要做这样的事情:
Ball ballHit = GetBallHit(); // Leaving it up to you to find out how to get whatever
// ball was just hit stored as a variable
if (BallManager.BallColorsSpawned[0].Color == ballHit.Color)
{
if (BallManager.BallColorsSpawned.Length == 1)
// That was the last ball remaining, so the player wins
else BallManager.BallColorsSpawned.RemoveAt(0); // Remove it as no longer important
}
else
{
// Player hit the wrong ball, so the player loses
}
如果你不知道 enum 是什么或扩展方法或其他我用过的东西,那么暂时停止使用 Unity,并使用 Visual Studio 上的实践项目、在线教程和乔恩·斯基特 (Jon Skeet) 的 C# 深度剖析。在我安装 Unity 之前,我已经编写了几个月的 C# 代码。