Search Unity

Getting NullReferenceException from Image array (SOLVED)

Discussion in 'Scripting' started by SheduMan, Aug 15, 2017.

  1. SheduMan

    SheduMan

    Joined:
    Aug 2, 2017
    Posts:
    5
    UnityInspectorWindow.png
    I have created an array of Images which have been set to image objects in the Editor Inspector. So I do not see how it is showing up as null in the console. Even though it throws an exception, it seems to execute correctly. This is the line of code where I declared the Image array.

    Code (CSharp):
    1.     public Image[] lifeImg = new Image[9];
    2.     public static sbyte livesLeft = 9;
    This function is used to set the images to a greyed out state when the player has lost a life. Lines 9 and 13 are where the NullReferenceExcepttions are occurring..

    Code (CSharp):
    1.     public void LifeCheck()
    2.     {
    3.         Color32 dead = new Color32(100, 100, 100, 200);
    4.         Color32 alive = new Color32(255, 255, 255, 255);
    5.         for (int g = 0; g < lifeImg.Length; g++)
    6.         {
    7.             if (g < livesLeft)
    8.             {
    9.                 lifeImg[g].color = alive;
    10.             }
    11.             else
    12.             {
    13.                 lifeImg[g].color = dead;
    14.             }
    15.         }
    16.     }
    The full error message is:
    NullReferenceException: Object reference not set to an instance of an object
    GameEngine.LifeCheck () (at Assets/Scripts/GameEngine.cs:36)
    GameEngine.Start () (at Assets/Scripts/GameEngine.cs:30)

    I believe I am just not understanding how to properly initialise the array so that it doesn't throw the error message. Any help would be appreciated.
     
    Last edited: Aug 15, 2017
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    1. public Image[] lifeImg;
    2. public static sbyte livesLeft = 9;
    that should fix it.
    public Image[] lifeImg = new Image[9]; // the "new Image[9]" is removing the values you set in unity.
     
  3. SheduMan

    SheduMan

    Joined:
    Aug 2, 2017
    Posts:
    5
    Thanks johne5 but I still get the error message although I found out that I didn't need the "new Image[9]. It ran just the same without it. I tried compiling and running the game as is and the error message seems not to make a difference in the game running. It ran just fine after being compiled.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    If you are still getting a null ref error, you need to fix that. It's not a good idea to just leave it because it doesn't seem to effect your game. Make sure none of the values are some how becoming null. Or that you don't have multiple copies of the script in your scene.
     
  5. dpgdemos

    dpgdemos

    Joined:
    Apr 28, 2014
    Posts:
    24
    I agree with @Brathnann, null reference errors should be remedied. They can cause funky issues for other scripts.

    In addition, have you tried setting break points in your loop to see exactly when the error is occurring?
     
  6. SheduMan

    SheduMan

    Joined:
    Aug 2, 2017
    Posts:
    5
    Hi guys and thanks for the input but the question is not about whether to fix it or not but how. Yes I have set break points and know the lines of code they occur on but not why they are null. Haven't figured that out yet. If I did I would fix it. You mentioned one thing about making sure there weren't multiple copies of the script which I will double check.
     
  7. SheduMan

    SheduMan

    Joined:
    Aug 2, 2017
    Posts:
    5
    Again, thanks to everyone's help. I have figured out a solution after reviewing a tutorial on arrays. It was all in getting it initialised correctly. I added the following code to my Awake function. It works like a charm with no error messages.
    Code (CSharp):
    1.     void Awake() {
    2.         //Initialize life count images
    3.         Allimages = Image.FindObjectsOfType(typeof(Image)) as Image[];
    4.         string imgName;
    5.         for (int j = 0; j < 9; j++)
    6.         {
    7.             int m = j + 1;
    8.             imgName = "Life" + m;
    9.             for (int k = 0; k < Allimages.Length; k++)
    10.             {
    11.                 if (imgName == Allimages[k].name)
    12.                 {
    13.                     lifeImg[j] = Allimages[k];
    14.                 }
    15.             }
    16.         }
    17.     }