- Home /
Simple Boolean Reset
Hello. I'm trying to make a script that only lets you create one shotSaw, then holds in in place while applying rotational force. However, it seems that the variable "charging" is not being set properly, and it spawns too many objects. Anyone know why it isn't being set if (charging != true)? I just have the other functions disabled in the code for now.
function FixedUpdate ()
{
if (Input.GetButton ("Fire1"))
{
if (charging != true)
{
var shotSaw = Instantiate (saw, transform.position, shootPosition.rotation);
var charging = true;
}
//shotSaw.rigidbody2D.AddTorque (10);
//shotSaw.transform.position = Vector3(shootPosition.position.x,shootPosition.position.y,shootPosition.position.z);
Debug.Log (charging);
Answer by robertbu · May 13, 2014 at 12:18 AM
The problem is one of two things depending the rest of your script. What you need to do is to put at the top of the file (if it is not already there):
var charging = false;
Then you need to remove 'var' on line 8. You are declaring 'charging' as a local variable to the FixedUpdate() function. You need it to be an instance variable. So Line 8 will be:
charging = true;
...with no 'var'.
Thanks, works great, don't know why I thought I needed var twice.
Your answer
![](https://koobas.hobune.stream/wayback/20220613144913im_/https://answers.unity.com/themes/thub/images/avi.jpg)