Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug Object Refrence not set to an instance of an object

Discussion in 'Scripting' started by ItsMe1423, Apr 27, 2024.

  1. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    Hello, I am getting this but I can't seem to understand why. if anyone can try and help it would be much appriciated
    This is the script where I am getting the bug at:
    Code (CSharp):
    1.     public void ChangeCurrentName(string input)
    2.     {
    3.         currentName = input;
    4.         currentIcon.Name = currentName;
    5.     }
    This is where I define both currentName and currentIcon:
    Code (CSharp):
    1.     [HideInInspector] public string currentName;
    2.     [HideInInspector] public Icon currentIcon;
    And this is the icon class:
    Code (CSharp):
    1.     public class Icon
    2.     {
    3.         private string name, type;
    4.         private Sprite image;
    5.         private int baseStr = 0, baseWis = 0, baseCons = 0, baseCha = 0, baseArc = 0, baseDex = 0, baseLvl = 0, baseStatPointsPerLvl = 0;
    6.  
    7.         public int BaseStr { get { return baseStr; } set { baseStr = value; } }
    8.         public int BaseWis { get { return baseWis; } set { baseWis = value; } }
    9.         public int BaseCons { get { return baseCons; } set { baseCons = value; } }
    10.         public int BaseCha { get { return baseCha; } set { baseCha = value; } }
    11.         public int BaseArc { get { return baseArc; } set { baseArc = value; } }
    12.         public int BaseDex { get { return baseDex; } set { baseDex = value; } }
    13.         public int BaseLvl { get { return baseLvl; } set { baseLvl = value; } }
    14.         public int BaseStatPointsPerLvl { get { return baseStatPointsPerLvl; } set { baseStatPointsPerLvl = value; } }
    15.         public string Name { get { return name; } set { name = value; } }
    16.         public string Type { get { return type; } set { type = value; } }
    17.         public int Id { get { return Id; } set { Id = value; } }
    18.         public Sprite Image { get { return image; } set { image = value; } }
    19.  
    20.         public Icon(Sprite img, string nme, string typ, int Identi, int baseStr, int baseWis, int baseCons, int baseCha, int baseArc, int baseDex, int baseLvl) { BaseLvl = BaseLvl; BaseDex = baseDex; BaseArc = baseArc; BaseCha = baseCha; BaseCons = baseCons; BaseWis = baseWis; BaseStr = baseStr; name = nme; type = typ; image = img; Id = Identi; }
    21.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,369
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,687
    You're defining a reference with this line of code not the actual object itself. By default a reference doesn't point to an object and is therefore
    null
    . You have to assign a reference either through code or the Inspector (disabled in this case). If you don't and you try to access it you'll always get the null reference exception.
     
    spiney199 likes this.
  4. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    I know this is not supposed to be an object
     
  5. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    I have found that once I try to access any of the icon properties in any way I get this error, I have been breaking my head around it and I no idea why do you maybe know why?
     
  6. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,474
    What Ryiah is saying is that you are never assigning the reference to currentIcon (at least not from the scripts you shared)

    So....your currentIcon reference is probably null and you are getting a Null Reference Error (hence the name) cause you are trying to access the Name property of currentIcon, but it doesnt exist yet, so currentName will never equal currentIcon.Name.
    upload_2024-5-1_12-42-4.png
     
  7. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    yes I have noticed that and changed
    Code (CSharp):
    1.     [HideInInspector] public string currentName;
    2.  
    to
    Code (CSharp):
    1.     [HideInInspector] public string currentName = "";
    2.  
    and it still doesn't work
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,195
    It's not working, because you're still missing what is actually null. Kurt has told you the steps on how to solve null errors. Rylah and Cornysam both tried to even tell you what is null.

    Where in your code do you assign a value to currentIcon? Where do you have "currentIcon =" in your code? You need to learn the difference between value types and reference types.
     
    Kurt-Dekker likes this.
  9. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    oh I haven't realised I needed to actually set up the currentIcon, I thought that I can set it up one by one, tnx all for your help I will try to be a bit more understanding next time lol
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,369
    Breaking your head around it is NOT one of the three steps.

    I'm serious here. This and EVERY nullref you EVER get forever into the future heat death of the universe will be solved by the same three steps posted above.

    Just to be complete about it:

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that

    The answer is always the same... ALWAYS!