[Solved] Changing inital value of guess for custom min/max range

I may be getting ahead of myself here, since I’m only on Lecture 19, but I was thinking of improving the NumberWizard game by allowing the user to select the range of numbers they could choose from, and automatically calculating the initial guess value as halfway between the two.

Keeping the 1-1000 range as a placeholder for now, I reasoned that you could express the initial guess value like this before the “void Start ()”:

int min = 1;
int max = 1000;
int guess = (max + min) * 0.5;

Instead, I’m get this error message instead:
“Assets/Scripts/NumberWizard.cs(9,22): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `NumberWizard.max’”

Given that line 9 is the “int guess” statement, something’s clearly wrong with the language I’ve used there. What would the correct way to write that line be?

1 Like

Hi @Felidus, absolutely nothing wrong with getting ahead of one’s self - great way to learn and push yourself :slight_smile:

The issue appears to be with something called NumberWizard.max, if I had to guess without seeing the code I would suggest there may be a missing bracket, curly brace or semi-colon somewhere.

Post up the full code for your NumberWizard.cs file and let’s see if we can resolve it together :slight_smile:

Hi Rob, good to know that I’m doing something right! The code as it stands so far is here: https://gist.github.com/anonymous/423a09185a635b760d9abfcb66973978
It works perfectly if I change line 9 back to “int guess = 500;”, which is why I assumed it was specifically something about C# not liking “(max + min) * 0.5”.

I wouldn’t be surprised if I missed a curly bracket somewhere, though. I’m coming to this course off a very basic knowledge of Python, which doesn’t use them like C# does, and I’m finding them a bit confusing. I understand that they indicate groups of statements, but they seem to be optional - the “if” and “else if” statements from lecture 18 seemed to work fine without them. This is a bit off-topic here, but are there specific rules to when they’re required, and how they’re supposed to be formatted?

1 Like

You may have a couple of issues here…

The fields are being defined within the class, but the class has yet to be initialised, as such, min and max effectively don’t exist, but you are trying to access them in guess when you state int guess = (max + min) * 0.5.

You may want to perhaps re-write it along the lines of this;

private int min;
private int max;
private int guess;

private void Start() {

private void Initialise() {
    min = 1;
    max = 1000;
    guess = (max + min) / 2;

The next issue you’d have received would be to do with the guess = (max + min) * 0.5; - you can’t multiply an int and a double like that. Hence the guess = (max + min) / 2 which will give you the same end result.

Regarding the curly braces { } - these will indicate code blocks, so, for an if / else if / else example;

if (guess == 100) {
    // guess equals 100, do something
else if (guess == 200)
    // guess did not equal 200, do something else
    // guess did not equal 100 or 200, do something completely different

It may have been the semi-colon you meant with regards to it not being required, however, within the above example it would be required at the end of a statement within those code blocks, e.g.

if (guess == 100) {
    Debug.Log("Guess equals 100!");  // semi colon at the end of the statement

Hope this helps :slight_smile:

Updated Wed Nov 23 2016 23:32

You could always cast the result of the calculation to an int I suppose, it gets a bit, well, brackety! :wink:

guess = (int)((max + min) * 0.5);

Wow, that was definitely a thorough answer! Thanks a lot, that seems to have fixed everything. Having to declare the variables first seemed a little weird at first, but now I’ve finished the whole section of the course I think I understand how and why it works!

I did have a weird minor bug when I re-wrote that section of the code along the lines of your version, though. When I tested it, each of the logs appeared in the console twice - when I started the game, it printed the four lines and then immediately printed them again, and so on. I should have taken a picture first, but closing and re-opening Unity and Monodevelop fixed the issue, and I haven’t been able to replicate it. Any idea what happened there?

Oh, and I’m using “guess = Random.Range (min,max);” now, but beforehand I did change it to “(max + min) / 2”. You’re right, it is a lot less brackety that way!

You could still set;

min = 1;
max = 1000;

as you were before, but it’s the guess variable where it tries to use them that is the problem, and, if you are going to have to have a bit of code, tucked nicely away in an initialisation method for that, you might as well use it for the other two as well :slight_smile:

hmmm, not off the top of my head, not without seeing all the code and the errors etc… sorry…

hehe, the KISS Principle :wink:

Privacy & Terms