1

我想从脚本中添加 GUITexture 但它是错误的,然后我尝试插入 GUITexture(从 GameObject > 创建其他 > GUI 纹理)然后将其链接到脚本,但是当我尝试使用 pixelInset 移动 guitexture 时,出现错误说

NullReferenceException UnityEngine.GUITexture.get_pixelInset () (在 C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/Graphics.cs:3254)

这是脚本

public GUITexture temp;

void Start () {
    temp = new GUITexture ();
    temp.pixelInset.Set (100,100,100,100);
}
4

2 回答 2

0

问题

当您调用new GUITexture()Unity 时,只会创建您的temp对象null。然后,当您尝试使用它时,您会得到一个NullReference Exception. 为什么?因为GUITexture继承自Behaviour,所以这正是 Unity 的构建方式。

解决方案

您已经通过编辑器将您的引用拖到GUItexture了,所以您只需删除此行。

// Remove this line
temp = new GUITexture ();
于 2015-01-02T06:39:23.240 回答
0

除了 FunctionR 的回答之外,在尝试对它做任何事情之前检查一个对象是否不为空总是好的。

public GUITexture temp;

void Start () {
    // temp = new GUITexture (); <- Overrides the texture your assigned in the editor
    if (temp) temp.pixelInset.Set (100,100,100,100);
}
于 2015-01-02T22:43:57.460 回答