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 :)
Answer by unidad2pete · Aug 18, 2017 at 08:57 PM
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.
Haha actually I just thought about that and went back here to answer it myself. And then, BOOO$$anonymous$$ you've already answered. Thanks :)
The problem, though, is that I get many errors of unexpected symbols. Like it doesn't like the curly brackets and what's inside. Any idea why?
Example : error CS1525: Unexpected symbol {' ; Unexpected symbol
,'
Sure, its only saying you of any side of your code there are any { or ; causing an error. Check your code to find all syntaxis and fix it, should be easy to find
Ah! my bad, I didnt see the problem, fix this:
void Start()
{
audioArray = new AudioSource[ audioFloat1, audioFloat2, audioFloat3 ];
clipArray = new AudioClip[ clipFloat1, clipFloat2, clipFloat3 ];
}
Yup, the errors have disappeared with this :) Thanks. I keep getting errors though (Cannot implicitly convert type UnityEngine.AudioSource' to
int') but I haven't proof read my code so I may have written something really weird somewhere.
Anyway, thanks again for the quick answers.
Ok, that's it :
private AudioSource[] audioArray;
private 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[clipPick].PlayOneShot(clipArray[clipPick]);
}
Thanks to @unidad2pete for the (very) fast and (very) efficient help :)
Your answer
Follow this Question
Related Questions
Get all Children of an Object with a certain component 0 Answers
How to get all children of a Gameobject with a certain component 2 Answers
IndexOutOfRangeException: Array index is out of range problem 2 Answers
Cant define array from other script 0 Answers
Cant define array from other script 1 Answer