1

I want to add GUITexture from script but it's error, then i try insert GUITexture (from GameObject > create other > GUI Texture) then link it to the script but then as i try move the guitexture using pixelInset, there is error said

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

Here's the script

public GUITexture temp;

void Start () {
    temp = new GUITexture ();
    temp.pixelInset.Set (100,100,100,100);
}
apxcode
  • 7,696
  • 7
  • 30
  • 41
Dream924Zz
  • 17
  • 5

2 Answers2

0

Problem

When you call new GUITexture() Unity will just make your temp object null. Then when you try to use it you get a NullReference Exception. Why? Because GUITexture inherits from Behaviour, it is just the way Unity is built.

Solution

You are already dragging your reference to the GUItexture via the editor, so all you need is to remove this line.

// Remove this line
temp = new GUITexture ();
apxcode
  • 7,696
  • 7
  • 30
  • 41
0

In addition to FunctionR's answer, it is always good to check if an object is not null before trying to do anything with it.

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);
}