Object Refrence not set to an instance of an object

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:

    public void ChangeCurrentName(string input)
    {
        currentName = input;
        currentIcon.Name = currentName;
    }

This is where I define both currentName and currentIcon:

    [HideInInspector] public string currentName;
    [HideInInspector] public Icon currentIcon;

And this is the icon class:

    public class Icon
    {
        private string name, type;
        private Sprite image;
        private int baseStr = 0, baseWis = 0, baseCons = 0, baseCha = 0, baseArc = 0, baseDex = 0, baseLvl = 0, baseStatPointsPerLvl = 0;

        public int BaseStr { get { return baseStr; } set { baseStr = value; } }
        public int BaseWis { get { return baseWis; } set { baseWis = value; } }
        public int BaseCons { get { return baseCons; } set { baseCons = value; } }
        public int BaseCha { get { return baseCha; } set { baseCha = value; } }
        public int BaseArc { get { return baseArc; } set { baseArc = value; } }
        public int BaseDex { get { return baseDex; } set { baseDex = value; } }
        public int BaseLvl { get { return baseLvl; } set { baseLvl = value; } }
        public int BaseStatPointsPerLvl { get { return baseStatPointsPerLvl; } set { baseStatPointsPerLvl = value; } }
        public string Name { get { return name; } set { name = value; } }
        public string Type { get { return type; } set { type = value; } }
        public int Id { get { return Id; } set { Id = value; } }
        public Sprite Image { get { return image; } set { image = value; } }

        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; }
    }

It is still the same as when I posted for you here:

The answer is always the same… ALWAYS!

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

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.

1 Like

I know this is not supposed to be an object

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?

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.
9807873--1408476--upload_2024-5-1_12-42-4.png

yes I have noticed that and changed

    [HideInInspector] public string currentName;

to

    [HideInInspector] public string currentName = "";

and it still doesn’t work

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.

1 Like

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

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!