【发布时间】:2015-06-28 03:12:05
【问题描述】:
我正在使用 Unity 4.3.3f1 并尝试在运行时加载纹理,但我不断收到 NullReferenceExpection。我的.png 位于this question 指定的资产/资源中。
我的实际代码是
Texture tex = Resources.Load("lava_pic", typeof(Texture)) as Texture;
Rect pos = new Rect(transform.position.x - 50,
transform.position.y - 50, 100, 100);
GUI.DrawTexture(pos, tex);
我也尝试过.png 扩展
错误是
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image, ScaleMode
scaleMode, Boolean alphaBlend, Single imageAspect) (at
C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/GUI.cs:208)
UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image) (at
C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/GUI.cs:204)
LavaScript.FixedUpdate () (at Assets/Scripts/LavaScript.cs:35)'
如果我输入Debug.Log(tex);,它也会打印出null,这就是问题所在。
我真的不知道为什么会这样。它在资源文件夹中,因为that's where it needs to be 并且我相当肯定它可以是.png 文件。我做错了什么?
【问题讨论】:
标签: c# null unity3d textures nullreferenceexception