- Home /
How to change a variable in the same script?
Hello, so I have this script attached to a gun my first person controller has. What it does is it plays an animation that zooms in and out the gun when the right mouse button is pressed. What i need it to do is when I first press the right mouse button, it plays the zoom animation on my gun and creates a variable called counter which has a int. value set to 1. Next, I need an if statement under function update that tests for is counter is = to 1. Then it tests if the right mouse button is clicked. If it is true, it will play a gun zoom out animation and reset counter to 0. I have a script below but I get the error, their is already a local variable, counter. The script is bellow in java script.
#pragma strict
var Gun : Transform;
function Update () {
if (Input.GetButtonDown("Fire2"))
{
var Counter : int = 1;
Gun.animation.Play("Gun Zoom");
}
if (Counter == 1)
{
if (Input.GetButtonDown("Fire2"))
{
var Counter : int = 0;
Gun.animation.Play("Gun Zoom Out");
}
}
}
Thanks in advance
Answer by karljj1 · Feb 22, 2015 at 07:11 PM
Your counter variable is hidden inside of the if statements scope. You need to define it outside and since its going to be used over more than one frame it should really have class scope.
#pragma strict
var Counter : int = 0;
var Gun : Transform;
function Update () {
if (Input.GetButtonDown("Fire2"))
{
Counter = 1;
Gun.animation.Play("Gun Zoom");
}
if (Counter == 1)
{
if (Input.GetButtonDown("Fire2"))
{
Counter = 0;
Gun.animation.Play("Gun Zoom Out");
}
}
}
you are still re-declaring counter (or is that not an issue in js?)
Oops yes. I copied the original code and missed the var :) Ill change it
When I'm using your script or Reder13's, it plays "gun zoom out" first and doesn't change counter but stays 0 all the time. On the other hand, the error isn't popping up anymore.
Thats because both if statements will be true.
This may work better for you:
if (Input.GetButtonDown("Fire2")) // Zoom in when the player presses the button
{
Gun.animation.Play("Gun Zoom");
}
if (Input.GetButtonUp("Fire2")) // Zoom out when they release the button
{
Gun.animation.Play("Gun Zoom Out");
}
Lol, that was actually what I was trying to achieve but i didn't know how to do that. Thanks.
Answer by Reder13 · Feb 22, 2015 at 07:13 PM
so something like this...
#pragma strict
var Gun : Transform;
var Counter : int = 0;
function Update () {
if (Input.GetButtonDown("Fire2")){
Counter = 1;
Gun.animation.Play("Gun Zoom");
}
if (Counter == 1)
{
if (Input.GetButtonDown("Fire2"))
{
Counter = 0;
Gun.animation.Play("Gun Zoom Out");
}
}
}
Answer by Glurth · Feb 22, 2015 at 07:14 PM
I'm no JS programmer, but I suspect the error has to do with the fact that the VAR statment means: allocate and use this variable name. the INT part says what kind of variable it is. This is called a variable "declaration"
You probably want to declare the variable in the class, or script, OUTSIDE of your update function. This way, it will retain it's assigned value between calls to update. (google the term "scope" for details)
Inside update, do not use the syntax to declare the variable, just use the syntax to assign a value to the (already declared) variable. probably something like Counter=0;
Your answer
Follow this Question
Related Questions
How do i make a character accelerate and play an animation when the click left-shift 1 Answer
Is it possible to create a script dinamic like animation>size controling the Elements variables? 1 Answer
variable has not been assigned? 1 Answer
Why can't I make this VAR public ? (Solved) 3 Answers
Javascript Movement Script. Why am I getting these errors? 2 Answers