I have some Unity code that's meant to load a bitmap image from a file and add it to a mesh. But I'm getting an ArgumentNullException
where I'm sure the argument isn't null.
The code which fails to load the image:
Texture2D img = new Texture2D(64, 64);
Debug.Log(">>" + img + "<<");
if (img == null) {
Debug.Log("img is null");
}
img.LoadImage(File.ReadAllBytes($"{Application.persistentDataPath}/textures/{sl}"));
But on that last line, I get an ArgumentNullException
.
The log output says this:
===============================================================
>> (UnityEngine.Texture2D)<<
UnityEngine.Debug:Log(Object)
Game:parseLine(String) (at Assets/Scripts/Game.cs:134)
Game:Awake() (at Assets/Scripts/Game.cs:78)
===============================================================
ArgumentNullException: Value cannot be null.
Parameter name: tex
UnityEngine.ImageConversion.LoadImage (UnityEngine.Texture2D tex, System.Byte[] data) (at /home/builduser/buildslave/unity/build/Modules/ImageConversion/ScriptBindings/ImageConversion.bindings.cs:35)
Game.parseLine (System.String line) (at Assets/Scripts/Game.cs:140)
Game.Awake () (at Assets/Scripts/Game.cs:78)ArgumentNullException: Value cannot be null.
===============================================================
I have verified twice
img
is a Texture2D
object img
was null. So the img
is a Texture2D
object that is not null, yet Unity seems to think it is.
Could someone please help me out on this one?
The error is for $"{Application.persistentDataPath}/textures/{sl}"
not the img
variable. Check application permissions and file path.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.