Newbie question: Weird issue accessing another object's variable!
Hi there!
I have (probably) a very simple issue. Basically I have a weapon switching script, and I'm trying to set a public bool to false on one of the weapons, but I can't get it to work :/
The weapon and script on the weapon are called "LightningGun", and it is a child of the object which has the weapon switching script. This is the code I'm trying to use:
LightningGun lg = GetComponentInChildren<LightningGun>();
lg.fireSoundAndMuzzleFlashPlayed = false;
As far as I can tell, this should work, but it gives me the error: "Object reference not set to an instance of an object."
I don't really understand why this isn't working because I'm using a very similar method to access a bool on the weapon switching script FROM the weapon and that works:
PlayerWeapons weapons = GetComponentInParent<PlayerWeapons>();
if (weapons.switching == false)
The only difference is that the one that doesn't work is trying to set the variable and the one that does work is just checking it. Maybe that's the issue? What am I missing?
Thanks :)
Answer by Pixelch3f · Sep 18, 2017 at 08:31 PM
Nevermind, worked it out:
public LightningGun lg;
lg.fireSoundAndMuzzleFlashPlayed = false;