Search Unity

Object Refernce not set to an instance of an object error.

Discussion in 'Scripting' started by Dustyman567, Oct 30, 2014.

  1. Dustyman567

    Dustyman567

    Joined:
    Oct 30, 2014
    Posts:
    5
    Hello. I am extreamly new to coding, but as far as I can tell this script should work.
    Code (CSharp):
    1.    
    2.     public class HeartsGUI2 : MonoBehaviour {
    3.        
    4.        
    5.         public GameObject Player22;
    6.        
    7.         public GUITexture image2;
    8.         public Texture2D Image_1;
    9.         public Texture2D Image_2;
    10.         public Texture2D Image_3;
    11.         public Texture2D Image_4;
    12.         public Texture2D Image_5;
    13.         public Texture2D Image_6;
    14.         public int hp = 5;
    15.        
    16.         public bool isEnemy = true;
    17.         void Awake()
    18.         {
    19.             Player22 = GameObject.Find("Player2");
    20.         }
    21.        
    22.        
    23.        
    24.         void Update()
    25.         {
    26.          if (Player22.GetComponent<Health>().hp == 5) {
    27.                 image2.guiTexture.texture = Image_1;
    28.             }
    29.             if (Player22.GetComponent<Health>().hp == 4) {
    30.                 image2.guiTexture.texture = Image_2;
    31.             }
    32.            
    33.             if (Player22.GetComponent<Health>().hp == 3) {
    34.                 image2.guiTexture.texture = Image_3;
    35.             }
    36.            
    37.             if (Player22.GetComponent<Health>().hp == 2) {
    38.                 image2.guiTexture.texture = Image_4;
    39.             }
    40.            
    41.             if (Player22.GetComponent<Health>().hp == 1) {
    42.                 image2.guiTexture.texture = Image_5;
    43.             }
    44.            
    45.            
    46.            
    47.             if (Player22.GetComponent<Health>().hp <= 0) {
    48.                 image2.guiTexture.texture = Image_6;
    49.             }
    50.            
    51.            
    52.            
    53.            
    54.             {
    55.                
    56.                
    57.                
    58.             }
    59.         }
    60.     }
    61.    
    62.    
    63.    
    Basically the code is supposed to change the image displayed on a hearts GUI every time Player2 is shot to show how much health it has left. I made the same code referencing my Player1 and it works fine but when I copy pasted it over and renamed everything it no longer worked. Unity came up with an enormous amount of errors all saying the same thing. NullReferenceException: Object reference not set to an instance of an object
    HeartsGUI2.Update () (at Assets/Scripts/HeartsGUI2.cs:28). Please help. Thank you.
     
  2. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Add
    Code (CSharp):
    1. if(!player22)
    2. {
    3. Debug.LogError("Couldn't find Player 2");
    4. }
    inside your Awake function after you try to assign Player22. If this shows in the console as an error, the gameobject "Player2" is not in the scene
     
  3. Dustyman567

    Dustyman567

    Joined:
    Oct 30, 2014
    Posts:
    5
    Ok I am an idiot for not noticing I accidentally named it Player 2 instead of Player2. The script still isn't working though although it now shows no errors. I don't understand why it doesn't work here but does on Player1. I will post my first script. See if you can find what the difference is between the 2. Thank you.
    Code (CSharp):
    1. using System.Collections;
    2.  
    3. public class HeartsGUI : MonoBehaviour {
    4.  
    5.  
    6.     public GameObject Player1;
    7.  
    8.         public GUITexture image;
    9.         public Texture2D Image_01;
    10.         public Texture2D Image_02;
    11.         public Texture2D Image_03;
    12.         public Texture2D Image_04;
    13.         public Texture2D Image_05;
    14.         public Texture2D Image_06;
    15.         public int hp = 5;
    16.      
    17.         public bool isEnemy = true;
    18.     void Awake()
    19.     {
    20.         Player1 = GameObject.Find("Player");
    21.     }
    22.      
    23.  
    24.  
    25.     void Update()
    26.     {
    27.         if (Player1.GetComponent<Health>().hp == 5) {
    28.                         image.guiTexture.texture = Image_01;
    29.                     }
    30.         if (Player1.GetComponent<Health>().hp == 4) {
    31.                         image.guiTexture.texture = Image_02;
    32.                     }
    33.                  
    34.         if (Player1.GetComponent<Health>().hp == 3) {
    35.                         image.guiTexture.texture = Image_03;
    36.                     }
    37.                  
    38.         if (Player1.GetComponent<Health>().hp == 2) {
    39.                         image.guiTexture.texture = Image_04;
    40.                     }
    41.                  
    42.         if (Player1.GetComponent<Health>().hp == 1) {
    43.                         image.guiTexture.texture = Image_05;
    44.                     }
    45.                  
    46.                  
    47.                  
    48.         if (Player1.GetComponent<Health>().hp <= 0) {
    49.                         image.guiTexture.texture = Image_06;
    50.                 }
    51.                      
    52.                      
    53.                      
    54.                      
    55.                         {
    56.                          
    57.                          
    58.                          
    59.                         }
    60.                     }
    61.                 }
    62.      
    63.              
    64.        
     
  4. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Probably the Health component is not attached to your player2.
    Just so you're aware also, you can store a reference to your Health component on Awake() also. Which will clean your code up a bit.

    Code (CSharp):
    1. using System.Collections;
    2. public class HeartsGUI : MonoBehaviour {
    3.     public GameObject Player1;
    4.       private Health m_health;
    5.         public GUITexture image;
    6.         public Texture2D Image_01;
    7.         public Texture2D Image_02;
    8.         public Texture2D Image_03;
    9.         public Texture2D Image_04;
    10.         public Texture2D Image_05;
    11.         public Texture2D Image_06;
    12.         public int hp = 5;
    13.  
    14.         public bool isEnemy = true;
    15.     void Awake()
    16.     {
    17.         Player1 = GameObject.Find("Player");
    18.        m_health = Player1.GetComponent<Health>();
    19.        if(!m_health)
    20.        {
    21.         Debug.LogError("Health component not attached");
    22.        }
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.        if (m_health.hp == 5) {
    28.                         image.guiTexture.texture = Image_01;
    29.                     }
    30.         if (m_health.hp == 4) {
    31.                         image.guiTexture.texture = Image_02;
    32.                     }
    33.              
    34.         if (m_health.hp == 3) {
    35.                         image.guiTexture.texture = Image_03;
    36.                     }
    37.              
    38.         if (m_health.hp == 2) {
    39.                         image.guiTexture.texture = Image_04;
    40.                     }
    41.              
    42.         if (m_health.hp == 1) {
    43.                         image.guiTexture.texture = Image_05;
    44.                     }
    45.              
    46.              
    47.              
    48.         if (m_health.hp <= 0) {
    49.                         image.guiTexture.texture = Image_06;
    50.                 }
    51.                  
    52.                  
    53.                  
    54.                  
    55.                         {
    56.                      
    57.                      
    58.                      
    59.                         }
    60.                     }
    61.                 }
    62.  
    63.          
    64.        
    Also, are all of your Image_ the same texture? Are you just trying to draw a heart for every hp you have?
     
  5. Dustyman567

    Dustyman567

    Joined:
    Oct 30, 2014
    Posts:
    5
    No. I have 6 images that I want to show on the GUI Texture depending on the health. My Health script is attached to player2 as well. I got the script to work now. Thank you for your help
     
    Last edited: Nov 4, 2014
  6. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    How are you drawing them? Maybe they are positioned on top of each other so it looks like only 1 set is drawing.

    Your code design is also not great. You should really only have 1 HeartsGUI component. Then your player class would reference this and feed it it's own health. When I get back from work I may give you an example code.