Not loading sprite correctly / Red question mark / Texture.LoadImage

Posting this here for a signal boost. Everything I’m finding on the subject is left unanswered and my project has ground to a halt because of this.

I have a block of code designed to locate an image in a file (“something”.png) and load it as a texture2d, then convert that texture2d in to a sprite for display on the user interface. My problem is that when it does, the sprite displayed is a question mark.

According to my reading, this means that the texture2d wasn’t created correctly. However, in testing my code, I know that it is loading the correct file, and it’s converting it to a sprite properly.

My question is, what am I doing wrong in the sense that the png file isn’t getting loaded correctly?

Function below:

public static Sprite LoadImage (ImageType type, string fileName)
     {
         Debug.Log (fileName); //it is getting passed the correct file name
         string targetDirectory = ImageTypeToString (type) + "/" + fileName + ".png";
         Debug.Log (targetDirectory); //the directory is being converted to the proper location
         byte[] fileData = File.ReadAllBytes (targetDirectory); //this line doesn't throw an error saying that it can't find the file, so it IS finding a file
         Debug.Log (fileData.Length); //returns the exact number of bits in the file (12342)
         Texture2D texture = new Texture2D (2, 2);
         texture.LoadImage (fileData); //wrapping this line in debug.log shows that it returns true, but it's still a ?
         Sprite newSprite = Sprite.Create (texture,
             new Rect (0, 0, texture.width, texture.height),
             new Vector2 (texture.width / 2, texture.height / 2),
             100f);
         return newSprite;
     }

Question in Answers location: http://answers.unity3d.com/questions/976854/question-mark-sprite.html

2136480--140809--Untitled.png

isn’t the path supposed to start with a “/”? or is the “ImageTypeToString()” function doing that already?

ImageTypeToString is taking an enum of the image type and returns the full path to that image type’s folder. For example, ImageType.Item returns the full path to the item image folder.

Application.dataPath + “/” + CurrentAssetPackage + “/” + ImageFolder + “/items”;

Then you can see the targetDirectory variable up there that gets the full path by adding in the file name.

Edit: My hunch is that there’s something wrong with the “file format” of the png. This hunch is due to the wording in WWW.LoadImageIntoTexture:

However, I’m not even using WWW.LoadImageIntoTexture. Instead, I’m using Texture2D.LoadImage. The latter has no mention of a red question mark whatsoever and I have no idea why this is happening.

If anyone has any information whatsoever that can point me in any kind of direction, it would be appreciated. I’m at a complete standstill on this and still haven’t found anything.

Is there something special about the PNG files that Texture2D.LoadImage needs? What, specifically, causes the red question mark texture to appear when using Texture2D.LoadImage? Perhaps more knowledge on how this function actually works would let me debug this further, or at least give me a hint.

Did you ever find an answer to this?

I’ve been facing the exact same issue and have not yet been able to reproduce
unity version 2018.4.0

waiting for answer!! aborting me long time!