Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Aurelio_hl2 · Aug 02, 2014 at 09:20 PM · arrayinvalidcastexception

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Aurelio_hl2 · Aug 02, 2014 at 10:09 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sytem.InvalidCastException on Windows Phone 1 Answer

"InvalidCastException" with MeshFilter from JS Array to builtin array. 2 Answers

Casting Error C# between Object[] and Texture2D[] 2 Answers

I can't see a reason for this Array.Reverse() error 3 Answers

InvalidCastException error and logic error 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges