Initializing array with colors

Hey dudes.

This line works fine

Color[] colors = { new Color(0, 1, 0, 1), new Color(1, 0, 0, 1), new Color(0, 0, 1, 1) };

But I want to initialize variables with colors first and then initialize the color array with them.
This one is not working

Color color1 = new Color(0, 1, 0, 1);
Color color2 = new Color(1, 0, 0, 1);
Color color3 = new Color(0, 0, 1, 1);
Color[] colors = { color1, color2, color3 };

Could you please explain me why?

Hi,

This should work:

Color[] colors = new Color[] { color1, color2, color3 };

Edit:
Actually your code too should work… it that your exact code? Are you sure you haven’t declared the colors array earlier and initialized it later.

Thanks for the answer. But I still get the same error with either my option or the one you suggested.

Error CS0236 A field initializer cannot reference the non-static field, method, or property ‘Ball.color1’

where Ball is the name of my public class MonoBehaviour

Share the full code here in code tags.

A global variable cannot reference another global variable when initializing its value:

public class Example
{
  int a = 10;
  int b = a; // CS0236
}

To do something like this, you need to initialize the other global variable from a method.

For plain C# classes, use a constructor:

public class Example
{
  int a = 10;
  int b;

  public Example()
  {
    b = a;
  }
}

For MonoBehaviours, use Awake or Start:

public class Example : MonoBehaviour
{
  int a = 10;
  int b;

  void Awake()
  {
    b = a;
  }
}

In your case:

public class Ball : MonoBehaviour
{
  Color color1 = new Color(0, 1, 0, 1);
  Color color2 = new Color(1, 0, 0, 1);
  Color color3 = new Color(0, 0, 1, 1);
  Color[] colors;

  void Awake()
  {
    colors = new Color[] { color1, color2, color3 };
  }
}

See the docs on this error for more info:

1 Like

Thanks a lot, Vryken and eses. Now it’s absolutely clear.

1 Like

Fun fact: Instance variable initializers can not reference each other because their execution order is not determined. Though static field initializers can reference other static fields but their initialization order is also undefined. However it “usually” follows the declaration order, but I would not count on it. See this fiddle as example.

Using static fields from other classes is usually not an issue as the static initialization of a class / types happens “lazily” when it’s first accessed. Though if you have static cross references you would again run into the same chicken and egg issue which you should avoid.

In theory the compiler could build a chain of dependencies which field initializer uses what other field. However this can easily cause issues. Also it would be possible to have some kind of cyclic dependencies which could not be resolved. When allowing fields to reference each other, what about properties? What if they are virtual? So a derived class could introduce a cyclic dependency where there was none in the beginning. This just doesn’t work :slight_smile:

Generally inside a field initializer of instance fields of a class you can not use any data or method from that class itself since it’s not initialized yet. Though you can use static fields or methods in instance field’s field initializers.

2 Likes