- Home /
Resources.Load texture problem
i want to make a pu system for my game and i want to desplay the icons of the power up and i've dont this
public class PowerUpContainer : MonoBehaviour {
public List<EBasePowerUp> PuList;
public Dictionary<string,Texture> icons=new Dictionary<string, Texture>();
// Use this for initialization
void Start () {
DirectoryInfo dir= new System.IO.DirectoryInfo("Assets/Textures/pu/");
FileInfo[] files= dir.GetFiles();
foreach(FileInfo f in files){
icons.Add(f.Name.Substring(0,f.Name.Length-4),(Texture)Resources.Load(f.FullName,typeof(Texture)));
Debug.Log("Loaded texture : "+f.Name);
}
}
// Update is called once per frame
void OnGUI () {
Texture icon;
Rect r=new Rect(10,100,30,20);
foreach(EBasePowerUp p in PuList){
if(icons.ContainsKey(p.GetType().Name)){
icon=icons[p.GetType().Name];
//........
r.x+=r.width;
if(r.x>90){
r.x=10;
r.y+=20;
}
}
}
}
}
but i have a problem, the texuter loades with no error, my dicionary contains the key so the if(icons.ContainsKey(p.GetType().Name)) is true... but the key value is null and i can't find the problem, the texture is loade without any error and is null... i have tryed to display all the textures that are in the dictionary and all are null... i think the problem is with "Resources.Load" but i'm a beginer in unity and i don't know so much
yes the files are loaded i have this debug log and its loading everything:
icons.Add(f.Name.Substring(0,f.Name.Length-4),(Texture)Resources.Load(f.FullName,typeof(Texture))); Debug.Log("Loaded texture : "+f.Name);
Answer by Esa · Mar 27, 2012 at 05:20 AM
Does the files Array
get populated? Put your textures into "Assets/Resources/..." folder, Resources.Load
always looks for them there.
Your answer
![](https://koobas.hobune.stream/wayback/20220613065541im_/https://answers.unity.com/themes/thub/images/avi.jpg)