- Home /
Getting list music array
Hi, am trying to fill array AudioClip:
using UnityEngine; using System.Collections;
public class Playlist : MonoBehaviour { AudioClip[] myPlaylist; int nextTrack = 0;
void Start()
{
myPlaylist = Resources.LoadAll("Music");
}
}
getting error: Cannot implicitly convert type UnityEngine.AudioClip' to
UnityEngine.AudioClip[]'
please tell me how to fix
P.S Sorry for bad english
Answer by Statement · Mar 23, 2011 at 10:26 AM
It actually doesn't say:
Cannot implicitly convert type 'UnityEngine.AudioClip' to 'UnityEngine.AudioClip[]' ...
But it does say:
Cannot implicitly convert type 'UnityEngine.Object[]' to 'UnityEngine.AudioClip[]'. An explicit conversion exists (are you missing a cast?)
The method return an array of UnityEngine.Object and you want them to be AudioClip. The easiest way I can think of is to use LINQ to cast the elements and then create an array of the result.
using UnityEngine; using System.Collections; using System.Linq;
public class Playlist : MonoBehaviour { AudioClip[] myPlaylist; int nextTrack = 0;
void Start()
{
myPlaylist = Resources.LoadAll("Music").Cast<AudioClip>().ToArray();
}
}
Your answer
Follow this Question
Related Questions
Login String to Bool Errors 1 Answer
Compile Errors for Script 1 Answer
Cannot implicitly convert type `UnityEngine.Object' to `UnityEngine.GameObject' 1 Answer
Accessing array in another script - Error message 2 Answers
instantiate object 1 Answer