in start variable is null and it gives error

Hi in my script in start ım accesing static variable herosave.locationnn[ ] and it gives

NullReferenceException: Object reference not set to an instance of an object
İnventory.Start () (at Assets/scripts/hero scripts/İnventory.cs:6)

and herosave.shotgunammo and herosave.shotgunammoongun dont give error and ı dont know why they dont give error because they are null too.

Because its null in start. İn updates static variables get values so how can ı prevent this error
by the way it gives error on line 6

void Start()
    {
   
        shotgunammo = herosave.shotgunammo;
        shotgunammoongun = herosave.shotgunammoongun;
       if(herosave.locationnn[0]!=null && herosave.locationnn[1]!=null)
        {

            x = herosave.locationnn[0];
            y = herosave.locationnn[1];

    transform.position = new Vector2(x, y);
        }
     
      
      
     
    }

    // Update is called onceesdwaframe
    void Update()
    {
        herosave.getlocationn(transform.position);
        herosave.shotgunammo = shotgunammo;
        herosave.shotgunammoongun = shotgunammoongun;
}

The locationnn array could be null. You don’t check this, you just assume it is filled.

1 Like

Fortunately it does not even matter what you are doing. The steps are ALWAYS the same:

How to fix a NullReferenceException error

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

Steps to success:

  • Identify what is null
  • Identify why it is null
  • Fix that

but it gives error.I dont understand what do you mean by you assume it if script can’t take the variable because it is null it gives nullreference exception;

herosave.locationnn is an array. An array itself can be null.

// By default not initialized, thus null.
int[] numbers;
// Trying to access array index 0.
// Will cause a null reference exception because numbers is not initialized.
if(numbers[0] > 1)
{
    // Do Something
}

// Null check on the array itself.
// If it isn't also check if the number is higher than 1
if(numbers != null && numbers[0] > 1)
{
}

If you’re new to programming, I’d suggest going through some basics.
https://learn.unity.com/tutorial/arrays-9o

2 Likes

I don’t know what you’re doing, what you’re trying, or even what you are talking about.

You are welcome to continue doing whatever you think you are doing.

I’ll say it again: whenever you are ready to fix your Null Reference, you can begin with these three steps.

How to fix a NullReferenceException error

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

Steps to success:

  • Identify what is null
  • Identify why it is null
  • Fix that

Thank you it works when ı check if array null but it does not work when ı check if an arrays value null
do you know why

 if(herosave.locationnn!=null)
        {
            x = herosave.locationnn[0];
            y = herosave.locationnn[1];
            transform.position = new Vector2(x, y);
        }

this works

 if(herosave.locationnn[0]!=null&& herosave.locationnn[1]!=null)
        {
            x = herosave.locationnn[0];
            y = herosave.locationnn[1];
            transform.position = new Vector2(x, y);
        }

but this not do you know why

An array is a reference type. It needs to be initialized before you can request the items from it.

Imagine sitting at a table and having a line of people.
You have to check whether the people are on the list. But you’re just there sitting at the table. There’s no list.
Now you’re being instructed to check the first person. How’re you going to do that without the list?
You’ve never been given the list of people to use to check with.

herosave.locationnn[0], you’re requesting the item at index 0 whilst the array is non existent. Null.
Why it is null? well that’s up to you really. You never initialized it in the first place.

You also might want to tag me next time. I’m not always watching all threads I answer to.

It is fascinating to me to see how long someone will resist doing the simple three-step process that works 100% of the time to fix a null reference error. I think one guy resisted it for a full week before finally doing the first step and then his nullref was fixed in like 60 seconds.

How to fix a NullReferenceException error

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

Steps to success:

  • Identify what is null <— waiting for you to do as soon as you want to fix this
  • Identify why it is null
  • Fix that

I’ll go grab my popcorn if it drags out any longer. This is fun!

@Kurt-Dekker fixing a null reference is one thing, understanding it is another.
And with understanding, I mean C# basics reference and value types.
Some just don’t grasp the idea that something can be null. If it were a value type it wouldn’t be null.
Then you also have people be like. But this is an array of int and an int is a value type. But the array still is a reference type.

Totally with you but you still must find what variable is null. That part is not optional. The longer that someone goes throwing up “this works but this does not” smoke screens, the longer they delay identifying what that null variable is.

I want to see OP get back to making progress in their game, not spending a day-plus on a nullref.

Why? Because it won’t be very long before the NEXT nullref comes along.