Hey, kind of new to c# but I keep running into this error (UnityException: You are not allowed to call this function when declaring a variable. Move it to the line after without a variable declaration. )

I’m trying to use an int from another class as an index for an array, I’m using Unity 5.3.2 and the array basically holds the health values for some enemies that will be instantiated at the start of wave. If anyone has any advice that would be great.

public float enStartingHealth;
public float enCurrentHealth;
float[] intHealth = new float[3] {//<=======================
50, 100, 200};
   
void Awake()
{        
    interceptor = GameObject.Find("Interceptor");
    enCurrentHealth = enStartingHealth;
}

void Update()
{
    WaveSpawner waveSpawner = FindObjectOfType<WaveSpawner>();
    enStartingHealth = intHealth[waveSpawner.enemyLvl];//<====================
}`

Probably do not want to assign those values every update for one. Instead maybe do a Initialize() method and make it public:

public float enStartingHealth;
public float enCurrentHealth;
float[] intHealth; // = new float[3] {//<=======================
 //50, 100, 200};

void Awake()
{
    intHealth = new float[3] { 50, 100, 200 };
    interceptor = GameObject.Find("Interceptor");
    enCurrentHealth = enStartingHealth;
}

public void Init(int startingHealthIndex) // When called from another script which has a reference to this script...
{
    WaveSpawner waveSpawner = FindObjectOfType<WaveSpawner>();
    enStartingHealth = intHealth[startingHealthIndex]; // the value handed to the method will be used here
    // this will set the "startingHealthIndex" that is handed to this method as the index which matches in the intHealth array.
}

// IN ANOTHER SCRIPTS SOMEWHERE...

void SomeOtherScriptMethod()
{
    DoStuff();
    MyClass myClass = GameObject.Find("SomeGameObject").GetComponent<MyClass>(); // Ideally you already have this reference!
    myClass.Init((int)someFloatThatsRound); // or just use an integer and don't cast!
}

You could use floats if that makes more sense in your game, and hand off a float to the method as well.