How to change the variables in my argument through a function?


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;



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

Try this:

P1.Difficulty = SetDifficulty(newValue);

Replace “newValue” with the new difficulty.

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);