1

I'm a newbie programmer and learn at the moment c# and Unity3d.

I got a problem, as i tried to initialize an array of public float variables.

    [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.)

I get the error: "A field initializer cannot reference the non-static field, method or property '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)."

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.

I'm feeling a little bit stupid, but I don't get the error :/

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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:

  • If it is OK with you to make probabilities static, make them static, or even const of they are readonly.
  • Otherwise, move initialization into the constructor of your class.

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};
}

1 Comment

Thanks a lot! So I can only use the static solution, if my class inherit from MonoBehaviour, because Unity doesn't like constructors. I know what a static method or static class is, but I haven't seen it at arrays, so I was confused.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.