Search Unity

Object reference not set to an instance of object issue.

Discussion in 'Scripting' started by BenHamin97, Apr 16, 2021.

  1. BenHamin97

    BenHamin97

    Joined:
    Apr 11, 2021
    Posts:
    4
    Hi there,

    I've been banging my head against the wall for hours on this one and I just cant seem to find the issue.
    I understand that what It means is I haven't linked up one of my game objects with the necessary references in the script.

    I've checked all the objects attached to each script many times and each one is where it should be.

    Basically I'm making a Pokémon style battle system and its been working flawlessly so far since I've been writing the code until now. The issue occurs when the game tries to update any component in the HUD and pulls up the error message:

    NullReferenceException: Object reference not set to an instance of an object
    BattleHUD.UpdateHP () (at Assets/Scripts/Battle/BattleHUD.cs:30)

    Now I know that my BattleHUD objects are correctly linked up to the script and I've checked this many times, here's a screenshot of the 'PlayerHUD' & 'EnemyHUD':

    EnemyHUD:
    Screenshot (2).png

    PlayerHUD:
    Screenshot (4).png

    I also know that both of these objects are correctly linked up to my BattleSystem script:
    Screenshot (6).png

    Here's my code of the coroutine within the BattleSystem script that then tries to call UpdateHP():
    Screenshot (10).png

    Here's my code for the UpdateHP Method (and the rest of the BattleHUD class):
    Screenshot (11).png

    And finally here's my code for the SetHP() Method that is called in the UpdateHP Method:


    I'm not sure if there's any issues in the code, only that visual studio is not showing any errors at all.
    I've tested the HP bar a while ago to make sure that it is able to be set so I know the code works in that sense.

    Any help would be greatly appreciated!
     
  2. BenHamin97

    BenHamin97

    Joined:
    Apr 11, 2021
    Posts:
    4
    5 image limit so here's the last screenshot (Code for SetHP Method):
    Screenshot (12).png
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    If your gui is correctly hooked up, then there is a good chance _champion is null. I don't see where you assign anything to that value. Also note, use Debug.Log to print out values of variables to see if they are null! Easy quick debugging tool.

    In the future, post code in the forums using code tags.
     
  4. BenHamin97

    BenHamin97

    Joined:
    Apr 11, 2021
    Posts:
    4
    Thanks for replying, Ill do some troubleshooting with debug. _champion can't be null though since the whole system draws upon it to calculate what happens in the battle, here's the code in the 'Champion' class:

    Code (CSharp):
    1. public class Champion
    2. {
    3.     public ChampionBase Base { get; set; }
    4.     public int Level { get; set; }
    5.  
    6.     public int HP { get; set; }
    7.  
    8.     public int AP { get; set; }
    9.  
    10.  
    11.     public List<Skill> Skills { get; set; }
    12.  
    13.     public Champion(ChampionBase pBase, int pLevel)
    14.     {
    15.         Base = pBase;
    16.         Level = pLevel;
    17.         HP = MaxHP;
    18.         AP = APValue;
    19.  
    20.         // Generate Moves
    21.  
    22.         Skills = new List<Skill>();
    23.         foreach (var skill in Base.LearnableSkills)
    24.         {
    25.             Skills.Add(new Skill(skill.Base));
    26.          
    27.             if (Skills.Count >= 4)
    28.             break;
    29.         }
    30.  
    31.     }
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Your _champion variable is null. The script isn't the issue. The variable is null. You don't ever assign a value to the variable. You pass in a champion value in the method above, but you don't assign it to _champion, which I think was your intent. You use it to update your gui, but that's it.
     
    mopthrow and Kurt-Dekker like this.
  6. BenHamin97

    BenHamin97

    Joined:
    Apr 11, 2021
    Posts:
    4
    Ohhhhhh i get it now, it's just clicked haha, thank you for your help!
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    :)
    Yep, that's why I mention Debug.Log. Standard process is go to the line the error is at and see what can be null. Debug.Log it before that line and see if you get any null values. Generally the easiest way to figure this error out.

    Good luck on your game. :)
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    I respect your diligence for keeping after it, but let me offer this shortcut to really save you some time in the future, because you will (like every other programmer in this world) get Nullrefs again:

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The three basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.
     
    mopthrow likes this.