- Home /
Array:Implicit downcast from 'Object' to 'UnityEngine.AudioClip'.
I use a script to differentiate states of sound and atmosphere without mixing The problem: Audioclip []; not add new elements as "Array.add" if allowed (or not as it is). when we take the audio clip at random, or just take a specific array audio clip, an error occurs and unity3d reports:
WARNING: Implicit downcast from 'Object' to 'UnityEngine.AudioClip'.
InvalidCastException: Cannot cast from source type to destination type.
Now the script
#pragma strict
var sound :Music[];
var NumX :int;
var NumY :int;
var AudioSor :AudioSource;
var Min :int = 2;
var Max :int = -2;
function Awake(){
for (var A:int=0;A<sound.length;A++){
if (Max < sound[A].track){Max = sound[A].track;}
if (Min > sound[A].track){Min = sound[A].track;}
}
}
function OnGUI(){
if (GUILayout.Button("++")){if (NumY < Max){NumY ++;}}
if (GUILayout.Button("--")){if (NumY > Min){NumY --;}}
}
function Update(){
if (!AudioSor.isPlaying){
var List = new Array (AudioClip);/*create a temporary Array only when needed*/
for (var A:int=0;A<sound.length;A++){
if (NumX == sound[A].track){List.Add(sound[A].Music);/*for we avoid conflicts between tracks, just add the music proved necessary*/}
}
AudioSor.clip = List[Random.Range(0,List.length)];/*randomly select a track*/
List = null;/*clean the list to not take up space in RAM*/
AudioSor.Play();
}
}
function FixedUpdate(){
if (NumX != NumY){AudioSor.volume -= 0.01;
if (AudioSor.volume <=0 || !AudioSor.isPlaying){
var List = new Array (AudioClip);/*create a temporary Array only when needed*/
for (var A:int=0;A<sound.length;A++){
if (NumX == sound[A].track){List.Add(sound[A].Music);/*for we avoid conflicts between tracks, just add the music proved necessary*/}
}
AudioSor.clip = List[Random.Range(0,List.length)];/*randomly select a track*/
List = null;/*clean the list to not take up space in RAM*/
NumX = NumY;
AudioSor.Play();
}}
if (NumX == NumY && AudioSor.volume <1){AudioSor.volume += 0.01;}
}
class Music{
var Music :AudioClip;
var track :int; /* 0=Music thriller , 1=Music for joy , 2=Horror music , 3=Music for relaxation */
}
How I can solve this problem? What is missing? Why the error happens? InvalidCastException: Can not cast from source type to destination type.
Answer by robertbu · Aug 02, 2014 at 09:24 PM
Use the .NET generic List class rather than the Array() class.
See the generic collection part of this page:
http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F
Ok Thanks for the link found an alternative way and works without errors, I just had to investigate a little more on my own
I substituted dashes
var List = new Array (AudioClip);
AudioSor.clip = List[Random.Range(0,List.length)];
// for
var List = new List.<AudioClip>();
AudioSor.clip = List[Random.Range(0,List.Count)];
and If anyone has similar problems, let the entire code
#pragma strict
import System.Collections.Generic;
var sound :$$anonymous$$usic[];
var NumX :int;
var NumY :int;
var AudioSor :AudioSource;
var $$anonymous$$in :int = 2;
var $$anonymous$$ax :int = -2;
function Awake(){
for (var A:int=0;A<sound.length;A++){
if ($$anonymous$$ax < sound[A].track){$$anonymous$$ax = sound[A].track;}
if ($$anonymous$$in > sound[A].track){$$anonymous$$in = sound[A].track;}
}
}
function OnGUI(){
if (GUILayout.Button("++")){if (NumY < $$anonymous$$ax){NumY ++;}}
if (GUILayout.Button("--")){if (NumY > $$anonymous$$in){NumY --;}}
}
function Update(){
if (!AudioSor.isPlaying){
var List = new List.<AudioClip>();/*create a temporary Array only when needed*/
for (var A:int=0;A<sound.length;A++){
if (NumX == sound[A].track){List.Add(sound[A].$$anonymous$$usic);/*for we avoid conflicts between tracks, just add the music proved necessary*/}
}
AudioSor.clip = List[Random.Range(0,List.Count)];/*randomly select a track*/
List = null;/*clean the list to not take up space in RA$$anonymous$$*/
AudioSor.Play();
}
}
function FixedUpdate(){
if (NumX != NumY){AudioSor.volume -= 0.01;
if (AudioSor.volume <=0 || !AudioSor.isPlaying){
var List = new List.<AudioClip>();/*create a temporary Array only when needed*/
for (var A:int=0;A<sound.length;A++){
if (NumX == sound[A].track){List.Add(sound[A].$$anonymous$$usic);/*for we avoid conflicts between tracks, just add the music proved necessary*/}
}
AudioSor.clip = List[Random.Range(0,List.Count)];/*randomly select a track*/
List = null;/*clean the list to not take up space in RA$$anonymous$$*/
NumX = NumY;
AudioSor.Play();
}}
if (NumX == NumY && AudioSor.volume <1){AudioSor.volume += 0.01;}
}
class $$anonymous$$usic{
var $$anonymous$$usic :AudioClip;
var track :int; /* 0=$$anonymous$$usic thriller , 1=$$anonymous$$usic for joy , 2=Horror music , 3=$$anonymous$$usic for relaxation */
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613155642im_/https://answers.unity.com/themes/thub/images/avi.jpg)