# How should I go about setting my highest number to a different value?

So I have some code that gets random numbers and then takes the highest, and I then want it to change the highest number to a 0. this is my code that does now work.

``````            One = Random.Range(1,6);
Two = Random.Range(1,6);
Three = Random.Range(1,6);
Four = Random.Range(1,6);
Five = Random.Range(1,4);

Mathf.Max(One,Two,Three,Four,Five) = 0f;
``````

The error I am getting it this: “(30,13): error CS0131: The left-hand side of an assignment must be a variable, property or indexer” (The last line is line 30). I understand what it means but do not know how to fix it. What other way should I assign the highest number to zero, so that my code actually works?

Hello, I’m afraid the way you want to use the method Mathf.Max doesn’t work for your case. If you want to iterate through multiple values, the best way to do that is to use a loop.

While this is a bit harder to understand at first, it saves you a lot of trouble later and makes your code easily extensible:

``````public int[] numbers = new int[5];

void Start()
{
int highestNumber = 0;

for (int i = 0; i < numbers.Length; i++)
{
numbers *= Random.Range(1, 6);*
``````

if (highestNumber < numbers_) highestNumber = numbers*;
}*_

for (int i = 0; i < numbers.Length; i++)
{
if (numbers == highestNumber) numbers = 0;
}
}
To clarify, first I create an integer array with a size of 5, just like you did with individual variables.
Then we iterate through all 5 values ​​in a for loop and assign a random value to each of them.
At the same time, we save the currently highest value in a separate variable, so that we know the highest value at the end of the loop.
In the second loop we go through all the variables again and set all that have the highest value to 0.
I hope I have understood your question correctly, because with this variant it can of course be the case, that more than one variable is set to 0.
It’s also maybe important to mention here, that if you use Random.Range with integers, then the max value is exclusive, which means if you use `Random.Range(1,6) ` you’ll get back values ​​from 1 to 5.
Variant number 2, if you don’t feel like the whole thing with arrays and loops:
One = Random.Range(1, 6);
Two = Random.Range(1, 6);
Three = Random.Range(1, 6);
Four = Random.Range(1, 6);
Five = Random.Range(1, 6);

int highestNumber = Mathf.Max(One, Two, Three, Four, Five);

if (One == highestNumber) One = 0;
if (Two == highestNumber) Two = 0;
if (Three == highestNumber) Three = 0;
if (Four == highestNumber) Four = 0;
if (Five == highestNumber) Five = 0;

“I understand what it means but do not know how to fix it”

What you’re doing is just setting the highest number to 0, which if its 5 its looks just like

5 = 0;

And that’s because you get the value of the number instead of the reference of that said value.

what you’d want to do first is to know which of the variables stores the highest number and then set its value to 0.

You could have different ways of doing so but here is my preference to this approach:

``````using System.Linq;

public void TestRandomNum()
{
Dictionary<string, float> Roll = new Dictionary<string, float>()
{
{"One",   Random.Range(1, 6) },
{"Two",   Random.Range(1, 6) },
{"Three", Random.Range(1, 6)},
{"Four",  Random.Range(1, 6)},
{"Five",  Random.Range(1, 4)}
};

//Gets the number that is the highest
float highestNum = Mathf.Max(Roll["One"], Roll["Two"], Roll["Three"], Roll["Four"], Roll["Five"]);

//Debug
print(\$"Highest num {highestNum}, numbers Before setting to 0 ({Roll["One"]},{Roll["Two"]},{Roll["Three"]},{Roll["Four"]},{Roll["Five"]})");

//Gets the highest numbers rolled and sets it to 0
Dictionary<string, float> HighestNumberRolls = Roll.Where(x => x.Value == highestNum).ToDictionary(x => x.Key, y => y.Value);

foreach(var numRoll in HighestNumberRolls)
{
Roll[numRoll.Key] = 0;
}

//Debug
print(\$"Highest num {highestNum}, numbers after setting to 0({Roll["One"]},{Roll["Two"]},{Roll["Three"]},{Roll["Four"]},{Roll["Five"]})");
}
``````

Notes

This will set every number if its the same as the highest one to 0

There might be better approaches depending on what you exactly want to do with it and if you want to set all of them to 0 or just once