Search Unity

Object reference not set to an instance of an object

Discussion in 'Scripting' started by BusyRobot, Jan 22, 2015.

  1. BusyRobot

    BusyRobot

    Joined:
    Feb 22, 2013
    Posts:
    148
    I'm trying to just setup a basic quad, I've found this code

    voidStart () {
    //Createaonepixeltexturewiththerightcolor
    varcolor = Color.red;
    intframeWidth = 10;
    Rectarea = newRect (Screen.height / 2, Screen.width / 2 + Screen.width / 8, 580, 64);

    vartexture = newTexture2D(1, 1);
    texture.SetPixel(0, 0, color);
    texture.Apply();

    RectlineArea = area;
    lineArea.height = frameWidth; //Topline
    GUI.DrawTexture(lineArea, texture);
    lineArea.y = area.yMax - frameWidth; //Bottom
    GUI.DrawTexture(lineArea, texture);
    lineArea = area;
    lineArea.width = frameWidth; //Left
    GUI.DrawTexture(lineArea, texture);
    lineArea.x = area.xMax - frameWidth;//Right
    GUI.DrawTexture(lineArea, texture);
    }

    But it's giving me this error

    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)
    UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image)
    SetupQuad.Start () (at Assets/SetupQuad.cs:19)
     
  2. SevenHams

    SevenHams

    Joined:
    Dec 27, 2012
    Posts:
    67
    According to that message the error is happening on line 19. Which line is that?
     
  3. BusyRobot

    BusyRobot

    Joined:
    Feb 22, 2013
    Posts:
    148
    It's the first GUI.DrawTexture(lineArea, texture);

    I'm using Unity on a Mac and everytime I try to debug to see the variables using Monodevelop, monodevelop crashes which isn't helping.
     
  4. SevenHams

    SevenHams

    Joined:
    Dec 27, 2012
    Posts:
    67
    There probably isn't an actual texture there then. Try creating an image and importing it then making that variable public and putting the image in the box that comes up in the editor. It doesn't need to be anything fancy; a 1x1 white pixel of an image will do. My guess is that it's trying to read an image but there isn't one there.
     
  5. BusyRobot

    BusyRobot

    Joined:
    Feb 22, 2013
    Posts:
    148
    I thought the line var texture = newTexture2D(1, 1); created a new blank texture?

    All I'm trying to do is create a blank (of a certain color) quad to appear.
     
  6. SevenHams

    SevenHams

    Joined:
    Dec 27, 2012
    Posts:
    67
    I feel like a dummy, the other thing I didn't notice was that you're using old GUI stuff in Start(). You can't do that. Legacy GUI stuff needs to be in OnGui(). It also looks like you aren't creating lineArea anywhere.
     
  7. BusyRobot

    BusyRobot

    Joined:
    Feb 22, 2013
    Posts:
    148
  8. SevenHams

    SevenHams

    Joined:
    Dec 27, 2012
    Posts:
    67
    Put the GUI calls in OnGui. That's legacy GUI, though. It would be good programming practice, however, for you to look at the new GUI documentation and figure out how to generate the texture and apply it to a GUI image on your own.