Resource.Load always returning null when trying to load an image
Hi all,
I've scoured the forums but I'm really unsure what's going wrong here - from what I can see, I'm following everything to the letter.
I'm making an application that that requires images to be dynamically brought in on startup, but I'm hitting a stumbling block where I'm unable to even bring in an image from the Resources folder at all.
I'm trying to load an image into a UI image in a canvas. I've assigned a placeholder so that I can easily see if it changes:
This image is then referenced in a parent controller GameObject and dragged in through the inspector. Here's the code in the controller that should load the image. I've bypassed a lot of code that makes it dynamic and am just trying to directly reference pork.jpg which is in the project resources folder.
public Image image;
...
void updateImage(string filename) {
//filename = filename.Substring(1, filename.Length - 2);
Sprite sprite = Resources.Load<Sprite> ("pork") as Sprite;
image.GetComponent<Image>().sprite = sprite;
if (sprite == null) {
Debug.Log ("Pork is null!");
}
//Debug.Log ("yay!" + Application.dataPath + "/Resources/img/" + filename);
}
Unfortunately, despite the fact that my sample image is in the root of the Resources folder (that's projectname/Assets/Resources), it just keeps returning null:
Again, I'm finding it difficult to see where I'm going wrong. I've tried countless combinations of the code, largely informed by responses to similar topics on this forum, but I'm still unable to get Resources.Load to actually load a sprite. This also happens with whatever sample image I try, regardless of file extension, and there are no error messages or anything.
Can anyone please give some recommendations? Ideally I want to be able to use Resources.Load so I can load in all the images at once and save on calls, but I haven't been able to get that far yet since I can't get it to work in the first place.
Thanks very much.
Answer by jgtoastie · Jul 25, 2017 at 09:15 AM
For the record, I ended up using the solution here: https://forum.unity3d.com/threads/generating-sprites-dynamically-from-png-or-jpeg-files-in-c.343735/ to bring images dynamically, since I couldn't find any other way to convert an image brought into the Resources folder to a sprite after the project had been republished.