- Home /
Change a variable via functions
Hello, I am having a problem in regards to changing variables. The code below shows two public integer variables, missilemaxrange and missileminrange which are set by default to 20 and 10 respectively. What I would like to do is to allow the player to set a different missile that has different max and min ranges. The following is the code I have to do that.
public int missilemaxrange = 20;
public int missileminrange = 10;
void Update ()
{
if (Input.GetKeyDown (KeyCode.A))
MissileSelectA ();
}
void MissileSelectA ()
{
int missilemaxrange = 100;
int missileminrange = 20;
{
Unfortunately the code is not working. Is there something missing in my code? I'm assuming that when the player presses the A key, the missilemaxrange and missileminrange variables are set to 100 and 20 respectively. This isn't happening, and apparently this isn't the way to change variables from a function, so what should I have done here? many thanks!
Thank you so much! It worked. I didn't know that could happen. many thanks
Answer by BiG · Jul 13, 2015 at 02:16 PM
By putting "int" next to missilemaxrange and missileminrange you are basicly re-declaring them! So you will have two different pairs of variables (with the same name but different scopes).
Remove those "int"s in lines 13 and 14 and you should be OK.
Your answer
Follow this Question
Related Questions
Sorting variable names by their values. 2 Answers
General question/advice for displaying multiple variables on a gameobject 0 Answers
One of my variable won't increase. [Answered.] 1 Answer
Distribute a total value from a variable to multiple other variables? 1 Answer
how to access variables in a function script attached to a clone 1 Answer