Referencing variables from another script

Hello,

I have a short question.
I’m trying to access variable from another script called “weapon”:
alt text

Everything works good, but when I change weapon (gameObjects with same Tag). I get error:
alt text

Is there any way to solve this? I have no idea how.

Thank you.

Your code looks up game objects tagged with Weapon. Maybe none are found, in which case your next line is going to go wrong. I suspect that is what is causing the NullReferenceException. Or, maybe the weapons variable does have a GameObject, but that GameObject doesn’t have a weapon component on it. In which case the Debug.Log line will cause a NullReferenceException. As I am sure you’ll know from reading all the questions on this site tagged with NullReferenceException this happens whenever a variable isn’t set to a value. You should never assume that a variable is assigned, so check that before using the variable.

Found my problem! Thank you!
I left parent taged with “weapons”. I removed tag and left only on childs, so it works.