Higher or Lower always changes the guess to 0

Hi Fadi,

What do you have the Higher and Lower buttons assigned to within the Inspector?

Also, screenshots are useful for the Unity editor and error messages, but for code, please copy/paste your code into your posts and tgen apply the code formatting characters before and after it.


See also;

the higher and the lower buttons are assigned the number wizard script as in the screenshots

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.SceneManagement;

public class NumberWizards : MonoBehaviour {
	int max;
	int min;
	int guess;
	public int maxGuessesAllowed = 10;

	public Text text;

	void Start () {
	StartGame ();
}	
	
	void StartGame () {
		int max = 1000;
		int min = 1;
		int guess = 500;
		max = max + 1;
}
	void NextGuess () {
		guess = (max + min) / 2;
		print ("Higher or lower than" + guess);
		maxGuessesAllowed = maxGuessesAllowed - 1;
		text.text = guess.ToString();
		if ((maxGuessesAllowed <=0)) {
			SceneManager.LoadScene("win");
		}
	}
	public void GuessHigher () {
		min = guess;
		NextGuess();
	}
	public void GuessLower () {
		max = guess;
		NextGuess();
	}	
}

Thanks for the info Fadi.

Did you drag the NumberWizards.cs script file from the Assets folder to the OnClick event?

yes I did.

I think you’ll find that’s the issue. You need to have your script attached to a GameObject, in this case your Number Wizard GameObject in the Hierarchy, and then drag that GameObject into the object field of the OnClick event.

When you do this, you should still see the public methods available in the drop down menu to the right, so you can select GuessHigher and GuessLower respectively.

The main difference is that unless your script is attached to a GameObject, it doesn’t execute, at least those which inherit from MonoBehaviour.

I think this is because you’re reinitialising the variables in the StartGame method. Because they are created in this method they are not the same ones as in the class itself so effectively they’re never set. Try taking “int” away from the front of max/min/guess in the StartGame method.

2 Likes

Didn’t work. It’s really odd

It’s attached to a game object (Number wizard) but I tried to drag that again to the on-click. didn’t work.

1 Like

…and there’s one I missed Martyn :wink:

I am surprised you are not seeing an error message about this Fadi?

What is happening is that your member variables, those defined inside the class, but outside of the methods are defaulting to their default values. In the case of an int this is 0 (zero).

You then have local variables within the StartGame method, as Martyn mentioned, these are being treated separately to those at the class level. It is only the local variables which you are effectively setting to have a value other than zero.

Later, when NextGuess is called, it sees only the class level variables, as those which were in StartGame were local to only that method. At this point, all of them are still at their default value of 0 (zero).

The good news is, your code is effectively working correctly, 0 + 0 = 0 and then divide that by 2… zero…

So, as Martyn says, remove the int declarations within the StartGame method and you should be sorted. Sorry I missed this earlier…

void StartGame () 
{
	max = 1000;
	min = 1;
	guess = 500;
	max = max + 1;
}
1 Like

So that’s 1 to me and about 200 to you… :wink:

2 Likes

I consider it more team work than a competition…

…but if you like :wink: :smiley:

1 Like

Thank you, both of you!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms