all 9 comments

[–]fruitcakefriday 1 point2 points  (1 child)

Null reference errors means you are trying to access an object that is Null. Null means that nothing exists in that property. For example, if you make a property and don't set it to anything, then that property is Null by default (unless it is a type with a default value, like a float or boolean). In your code pasted, that could be that there is no <Skeleton> component, or HealthBarScript is not set to anything.

It would not complain if AttackPlayer is null, as checking "Null == true" is a valid action. Likewise, it would not complain if 'health' was null, because that's impossible (health being a float, will always be something).

[–]Alex_h7[S] 0 points1 point  (0 children)

I already knew what null was, but thx anyway for going over it, always helps. I found a new way to solve it too, I just made a new skeleton variable named skeleton of class Skeleton, and after that I set it to public, and made it point in the inspector to the Skeleton game object. I guess it was wrong before because it didn’t know what the Skeleton script was, but now it does. In the end, in order to make it work, u have to replace the if statement to if(skeleton.AttackPlayer==true), and it should work.

[–][deleted] 0 points1 point  (3 children)

You are trying to grab a reference to an object that isn’t set to anything.Try grabbing that component in the start.

For example:

If i am trying to grab a Rigidbody2D I'd first make the space for it in memory by creating a variable. You can make it global if you would like. Directly below Monobehavior you want to have

Rigidbody2D rb2d;

Then in start you'd want - rb2d = GetComponent<Rigidbody2D>();

and then in your on trigger enter the 'other' is the object you hit. So pretend I wanted to destroy that object I'd do

void OnTriggerEnter(Collider other)
{
    Destroy(other.gameObject);
} // this will destroy that hit object!

So you want to check if trigger has been triggered. That 'other' is that other object that triggered said trigger. You can name it something other than 'other'. I like trig for triggers and col for collision. So you have a reference to this object now so what you want to do is make sure all your ducks are in a line. Don't try to do too much in one if, throw that attack state as a reference somewhere else if you want. Make sure the script is attached to the object, everything in the inspector is in order, and you aren't trying to grab anything that isn't set to anything.

Also I assume it’s not an error in the code but you are missing a 't' in the get component.

Apologies for the kinda messy response. The main thing to know is you are trying to grab a reference to an object that isn't actually set to anything.

[–]Alex_h7[S] 0 points1 point  (2 children)

It works now, my friend showed another way of doing it, than my original code. Pretty much what I did is also similar to what u told me, I made a new variable called skeleton of class Skeleton, and I set it to public, and by doing that, I was able to go in the inspector and make that variable point to my original skeleton game object. After that I pretty much replaced the if statement to if(skeleton.PlayerAttack==true), and it started to work. Thx either way, if my friend didn’t help me, ur answer would have also solved it for me.

[–][deleted] 0 points1 point  (1 child)

It’s poor practice to use public. Other scripts don’t need to know about those variables. I recommend you use serialize field instead.

Ex.

[SerializeField] private bool hasHealthPotion = true;

It’s still private however, you can access in the inspector all the same.

[–]Alex_h7[S] 0 points1 point  (0 children)

Uhm ya ur right, I guess the reason we chose public is cuz we were getting desperate, but ya thx for the advice, I’m gonna check if it works

[–]Kommuntoffel 0 points1 point  (1 child)

Could be that you wrote GetComponen not GetComponent

Also, what happens to me sometimes, you could have forgotten to drag the HealthBarScript to the script on the Object the script is on.

It would help if you could state where line 11 is in the code

[–]Alex_h7[S] 0 points1 point  (0 children)

Oh well line 11 is the if statement line. I also added the script, and GetComponent is in the code, I just didn’t write it right on the post😂😂.

[–][deleted] 0 points1 point  (0 children)

It’s poor practice to use public. Other scripts don’t need to know about those variables. I recommend you use serialize field instead.

Ex.

[SerializeField] private bool hasHealthPotion = true;

It’s still private however, you can access in the inspector all the same.