Unity3D – warning Messages – BCW0028: WARNING: Implicit downcast from ‘UnityEngine.Component’ to ‘UnityEngine.AudioSource’
Implicit Downcast from… to…
BCW0028: WARNING: Implicit downcast from ‘UnityEngine.Component’ to ‘UnityEngine.AudioSource’
This script will generate the BCW0028 error:
... // buttons[i] -> this is an Array of GameObject ... // Assegno Audio INIZIO ############################################################################# function AssignAudio () { var audioSrc : AudioSource; for(var i : int = 0; i < buttons.Length; i++) // assegno a tutti gli oggetti nell'array l'audio { // BCW0028: WARNING: Implicit downcast from 'UnityEngine.Component' to 'UnityEngine.AudioSource'. audioSrc = buttons[i].AddComponent ("AudioSource"); buttons[i].audio.clip = buttonAudio0; // Add audio clip buttons[i].audio.playOnAwake = false; // not play on awake } }// END AssignAudio() // Assegno Audio FINE ############################################################################### ...
Solution: you should explicity cast the Components to AudioSource yourself using “as” like this:
Change the row:
audioSrc = buttons[i].AddComponent ("AudioSource");
with
audioSrc = buttons[i].AddComponent ("AudioSource") as AudioSource;