[英]c# array initialization “cannot reference the non-static field, method or property” error
I'm a newbie programmer and learn at the moment c# and Unity3d. 我是一名新手程序员,目前正在学习c#和Unity3d。
I got a problem, as i tried to initialize an array of public float variables. 我尝试初始化公共float变量数组时遇到问题。
[Range (0,1)]
public float appleProbability = 0.4f ;
[Range (0,1)]
public float fishProbability = 0.2f ;
[Range (0,1)]
public float cheeseProbability = 0.10f ;
[Range (0,1)]
public float poopProbability = 0.14f ;
[Range (0,1)]
public float bombProbability = 0.14f ;
[Range (0,1)]
public float starProbability = 0.02f ;
private float[] probs = new float[] {appleProbability, fishProbability, cheeseProbability, poopProbability, bombProbability, starProbability};
(The [Range (0,1)] should make a slider in the inspector of the script in unity, so you can manipulate the public variable between 0 and 1 with the slider.) ([Range(0,1)]应该在脚本检查器中统一创建一个滑块,以便您可以使用滑块在0和1之间操纵公共变量。)
I get the error: "A field initializer cannot reference the non-static field, method or property 'GameManager.appleProbability'." 我收到错误消息:“字段初始化程序无法引用非静态字段,方法或属性'GameManager.appleProbability'。” (the same for the other variables)
(其他变量相同)
I tried this code for a test: 我尝试了以下代码进行测试:
public int blub = 1;
public int hub = 2;
private int[] bla = new int[3];
bla[0] = blub;
but I get the error: "Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)." 但出现错误:“无法在变量声明中指定数组大小(尝试使用'new'表达式进行初始化)。”
Strange thing is, that the first error dissapears, when I get the second error, also I haven't changed the code from the first error. 奇怪的是,第一个错误消失了,当我遇到第二个错误时,我也没有从第一个错误更改代码。
I've read this question all possible c# array initialization syntaxes but it doesn't help. 我已经阅读了所有可能的c#数组初始化语法的问题,但无济于事。
I'm feeling a little bit stupid, but I don't get the error :/ 我有点愚蠢,但我没有收到错误:/
The compile tells you precisely what's happening - you cannot reference non-static fields in the array initializer. 编译可精确地告诉您正在发生的事情-您无法在数组初始化程序中引用非静态字段。
You have two options to resolve this: 您可以通过以下两种方法解决此问题:
static
, make them static
, or even const
of they are readonly. static
,请将它们设为static
,甚至将它们的const
设为只读。 Here is the first option: 这是第一种选择:
[Range (0,1)]
public static float fishProbability = 0.2f;
...
Here is the second option: 这是第二种选择:
private float[] probs;
public MyClassConstructor() {
probs = new float[] {appleProbability, fishProbability, cheeseProbability, poopProbability, bombProbability, starProbability};
}
you are trying to set values into an array when those values have not been initialized, you need to either make those values static, hard-code defaults in the array, or initialize those values in the constructor: 您试图在未初始化值的情况下将值设置为数组,则需要将这些值设置为静态,在数组中使用硬编码默认值,或者在构造函数中初始化这些值:
[Range (0,1)]
public static float appleProbability = 0.4f ;
[Range (0,1)]
public static float fishProbability = 0.2f ;
[Range (0,1)]
public static float cheeseProbability = 0.10f ;
[Range (0,1)]
public static float poopProbability = 0.14f ;
[Range (0,1)]
public static float bombProbability = 0.14f ;
[Range (0,1)]
public static float starProbability = 0.02f ;
private float[] probs = new float[] {
appleProbability,
fishProbability,
cheeseProbability,
poopProbability,
bombProbability,
starProbability};
OR: 要么:
[Range (0,1)]
public float appleProbability = 0.4f ;
[Range (0,1)]
public float fishProbability = 0.2f ;
[Range (0,1)]
public float cheeseProbability = 0.10f ;
[Range (0,1)]
public float poopProbability = 0.14f ;
[Range (0,1)]
public float bombProbability = 0.14f ;
[Range (0,1)]
public float starProbability = 0.02f ;
private float[] probs = new float[] {
.4f,
.2f,
.10f,
.14f,
.14f,
.02f};
OR: 要么:
[Range (0,1)]
public float appleProbability = 0.4f ;
[Range (0,1)]
public float fishProbability = 0.2f ;
[Range (0,1)]
public float cheeseProbability = 0.10f ;
[Range (0,1)]
public float poopProbability = 0.14f ;
[Range (0,1)]
public float bombProbability = 0.14f ;
[Range (0,1)]
public float starProbability = 0.02f ;
private float[] probs;
MyClass()
{
probs = new float[] {
appleProbability,
fishProbability,
cheeseProbability,
poopProbability,
bombProbability,
starProbability};
}
You could also make them const
instead of static if they are not going to change. 如果不更改它们,也可以使它们成为
const
而不是static。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.