- Home /
How to reset a variable when activated
Im making a FPS and I have several guns. i set it so when the guns are equipped, there is a variable that is deactivated/activated at certain times so the player cant shoot while reloading, or equipping.
I made the gun start where it cant shoot, because the fraction of a second difference between the gun being activated and the animation playing allows the player to shoot, and then switch guns, so then the gun he switched to will start shooting without being equipped. The varibale "abletoshoot" starting at 0 makes the player unable to do this.
The problem is that this only works the first time the weapon is equipped, because "abletoshoot" is not converted back to 0. I was wondering if there is a function thast I can call so when the weapon is activated, set it to 0, or deactivated, set it to 0.
is there such a function to call? activated or deactivated, or possibly something I can use to reset the variable when the weapons are activated or deactivated.
Answer by Goody! · Jan 12, 2011 at 12:52 AM
Just happened across this question and can't resist answering. I like easy questions. ;)
Put a Boolean in the script that is flipped back and forth from true to false and back again. Like ableToShoot = true/false That seems more straightforward than setting a number.
This post (below) helped me out quite a bit back a while ago. It should show how to set up bools.
well I have the abletoshoot setup and it works fine, but the problem comes in when the weapon is re-activated. This is how it guys
Weapon activated : abletoshoot = 0 until animation finishes weapon deactivated : abletoshoot = 1 so it can shoot weapon re-activated : abletoshoot = 1, it starts off at 1 so the player can shooot while equipping.
I was wondering if there is some way I can reset abletoshoot back to 0 when the weapon is deactivated. How would a boolean work for this? (sorry i have never used booleans before)
@unknown(google), I'd recommend checking out the link I posted above called AwesomeTutorial about scripting. It's long but the reality of Unity is that unless you understand scripting, including booleans, or have someone to do the scripting for you, you won't get far with it. Sorry. :) You can try posting your script up here but most people here won't write it for you if they don't think you have at least a basic grasp on things.
Your answer
Follow this Question
Related Questions
Can't activate my deactivated GameObjects 1 Answer
Is there any command that can reference deactivated objects? 1 Answer
How to activate a gameobject via scripting ??? 3 Answers
Using OnSceneLoad (additive) to activate script on player within active scene 0 Answers
deactivate and active object, problems 2 Answers