gpt4 book ai didi

c# - Array.FindIndex 在统一 C# 中始终为空

转载 作者:行者123 更新时间:2023-11-30 17:34:34 26 4
gpt4 key购买 nike

我不知道为什么我下面的 unity c# 脚本总是 null ?

public class WeatherControl : MonoBehaviour {
public GameObject Rain;

public int[] RainTime = new int[]{6,7,8,9,10,18,19,20,21,22,16};

int day;
System.DateTime dates;

// Use this for initialization
void Start () {
dates = System.DateTime.UtcNow;
day = (int) dates.Day;
//day = 16;
Debug.Log ("DAY : " + day);
int posRain = System.Array.FindIndex (RainTime, x => x.Equals(16));

Debug.Log ("POS RAIN : " + posRain);
if (posRain >= 0) {
Rain.SetActive (true);
} else {
Rain.SetActive (false);
}

}

}

我的变量 int posRain 总是return -1 即使在数组中也有包含它的值。可变日包含 16。我手动输入并输入 16。但总是返回-1。我不知道为什么。

我也试过了:

int posRain = System.Array.IndexOf (RainTime, day);

那也总是 return -1。

我已经在此处的在线 C# 测试器上对其进行了测试:https://csharppad.com/

它在返回 10 时有效。

但在 unity c# 编辑器中它总是返回 -1 是不同的。

谁能解释一下这是怎么回事?

谢谢

丹尼斯

最佳答案

您的数组是公开的,因此由 Unity 序列化。 Unity 会将数组设置为您在检查器中给它的值,覆盖您在代码中声明的值。如果您不需要从检查器访问数组,您应该使用 [NonSerialized] 属性。如果您确实需要从检查员访问它,您也应该从那里编辑它。

关于c# - Array.FindIndex 在统一 C# 中始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42268370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com