Error CS0103 : Array name "does not exist" if I initialize it in Start function

Basically, I had an two arrays that I initialized outside of a function and I got the error :

"field initializer cannot reference the nonstatic field, method, or property… ".

Everything else was fine. I moved the initialization into my Start function, and now I have the error

“The name `audioArray’ does not exist in the current context”.

I’ve checked for a typo, but I doubt it since that was fine before I moved it to Start (). I’m probably missing something big.

void Start () {
		AudioSource[] audioArray = {audioFloat1, audioFloat2, audioFloat3};
		AudioClip[] clipArray = {clipFloat1, clipFloat2, clipFloat3};
	}



	void playRandom(){
		int clipPick = Random.Range(0, audioArray.Length);    //audioArray is displayed in red
		audioArray[clipPick].PlayOneShot (clipArray[clipPick]);     //audioArray and clipArray are displayed in red
	}

It’s the first time I have that problem without seeing what’s wrong and I’m really confused… Can you help me ?

Any help is appreciated :slight_smile:

On your code, you are creating Arrays on local way, only works inside on function on was created, in this case Start(), try this.

    public AudioSource[] audioArray;
    public AudioClip[] clipArray;

    void Start()
{
    audioArray = new AudioSource[ audioFloat1, audioFloat2, audioFloat3 ];
    clipArray =  new AudioClip[ clipFloat1, clipFloat2, clipFloat3 ];
}



    void playRandom()
    {
        int clipPick = Random.Range(0, audioArray.Length);    //audioArray is displayed in red
        audioArray[clipPick].PlayOneShot(clipArray[clipPick]);     //audioArray and clipArray are displayed in red
    }

Now, the array is knowed by all script, but defined on Start function.