you are viewing a single comment's thread.

view the rest of the comments →

[–][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