How to change the variables in my argument through a function?
Hello
I am currently trying to change a variable, which is in the argument of my function, and set the value of the variable in my argument to the value of the variable in the function. Apologies if I have not clearly articulated my question. Here is an example.
Here are 2 segments of my code :
float SetDifficulty (float Difficulty)
{
Difficulty = (float)(Stage)-0.5f;
Debug.Log("Difficulty : " + Difficulty);
return Difficulty;
}
and
SetDifficulty(P1.Difficulty);
What I am trying to do is to set P1.Difficulty to what the result of Difficulty is in my function. In my console, Difficulty is 0.5 in the function SetDifficulty, however P1.Difficulty is always 0.
If anyone can assist, it would be a great help.
Thank you in advance
Answer by SarperS · Jun 22, 2016 at 08:48 AM
You have 2 options. Either assign the returned value or use the "ref" keyword.
Option 1
float SetDifficulty() {
return (float)(Stage)-0.5f;
}
P1.Difficulty = SetDifficulty();
Option 2
void SetDifficulty (ref float Difficulty) {
Difficulty = (float)(Stage)-0.5f;
}
SetDifficulty(ref P1.Difficulty);
option 2 needs the "ref" key words as well when you call the method:
SetDifficulty(ref P1.Difficulty);
This, and much more, is in any "Intro to C#" book. I've had people tell me the HeadsUp(?) series is decent.
Answer by ETech-Studios · Jun 22, 2016 at 03:58 PM
Try this:
P1.Difficulty = SetDifficulty(newValue);
Replace "newValue" with the new difficulty.
Your answer
Follow this Question
Related Questions
How to assign slider to float: Spin, on a gameobject 1 Answer
Variable from Void not working 1 Answer
How to return a function 0 Answers
I need to activate a function in another script from a script on a different game object 0 Answers
How to give Vector3 position to object being instantiated 1 Answer