- Home /
List error c#
I am trying to load images in my resource folder so that I can cycle through them during run time to animate 2D characters. I am getting an error when I try to Add the textures into the array. I'm new to this List...normally I use arrays. Anyone see the mistake?
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class ImageAnimation : MonoBehaviour
{
public string fileName;
public bool loop;
private float pictureRateInSeconds = 0.04166666666666666666f;
private List<Texture2D> imagePictures;
private int imageCounter;
void Start()
{
imagePictures = new List<Texture2D>();
}
void InitializeImage(string fileName)
{
Object[] imageTextures;
imageTextures = Resources.LoadAll( "TestImages", typeof(Texture2D));
for(var i = 0; i < imageTextures.Length; i++)
{
imagePictures.Add(imageTextures[i] );
}
}
Answer by Huacanacha · Nov 09, 2013 at 05:47 AM
It always helps if you list the exact error you're getting along with the code.
In this case I can see that imageTextures is an Object[] array, whilst List obviously expects Texture2D types. I'm not sure exactly how Resources.LoadAll works (whether there is a generic version that will return Texture2D's etc) but you can just cast the texture when you pull it from imageTextures:
imagePictures.Add((Texture2D)imageTextures[i]);
Edit: if that's not it, list the error you're getting so we know what the actual problem is :)