Section4 Lecture 51



Hi there i hope someone can help me to fix this problem;
the number is not going higher neither lower, it is totally disappearing when i click on “Higher” or “Lower”. While i added all the functions, same as in the course video.
Or it is happening because of the new version of Unity5.6, i had another script error anyways Unity5.6 is not available right now on the site, i heard that; it is already released/published. So if anyone know where i can download the new version. Please send me the link.
Thanks.

Hello @Sabeen_Moin,

I think it’s unlikely this is a Unity 5.6 issue.

Looking at the Scene view, can you select one of your buttons (lets start with the Higher one), and with the Inspector on display, take a screenshot and post it please.

Also, can you post your full NumberWizard.cs script please.

Regarding the download of Unity 5.6, it appears to be located as expected for myself, on the Unity Download Archive page.


See also;

1 Like

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

public class NumberWizard : MonoBehaviour {

	int max;
	int min;
	int guess;

	public int maxGuessesAllowed = 5;
	public Text text;

	void start () {
		StartGame();
	}

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

		max = max + 1;
	}

	public void GuessLower () {
		max = guess;
		NextGuess();

	}

	public void GuessHigher() {
		min = guess;
		NextGuess();

	}

	public void NextGuess() {
		guess = (max + min) / 2;
		text.text = "";
		maxGuessesAllowed = maxGuessesAllowed -1;
		if (maxGuessesAllowed <= 0) {
			Application.LoadLevel("Win");
		} 
	}
}

:sweat:
Here is the script of Number Wizard. And i found unity5.6. Thank you.

1 Like

Hi @Sabeen_Moin,

Ok, so the buttons are wired up to the script correctly, that’s good.

Running through in order of events so you can spot the problem…

  • Start() is called on NumberWizard.cs
  • StartGame() is called from Start()
  • Initial variable values are set

Note, at this point 500 is only displayed on the screen because it is set in the Inspector, although you don’t have a screenshot of that here, if you select the Text game object you will see that it’s Text property is set to 500.

  • You then click either the Higher or Lower button and the corresponding methods are called, GuessHigher() / GuessLower().
  • In both of these methods they set a variable and call NextGuess()

So far we’ve not found anything to do with populating your Text game object on the screen, lets keep going…

  • Within NextGuess(), on the second line you set the text value of the Text game object to “” (e.g. nothing)

This is why your 500 value disappears and is not replaced with anything else.

public void NextGuess() {
	guess = (max + min) / 2;
	text.text = "";  // <-- this line needs to have a variable set so that it is displayed
	maxGuessesAllowed = maxGuessesAllowed -1;
	if (maxGuessesAllowed <= 0) {
		Application.LoadLevel("Win");
	} 
}

You might also consider renaming your Text game object to perhaps Guess or WizardsGuess so that in code it would read, guess.text or wizardsGuess.text - might be easier to follow with the word “text” appearing less. :slight_smile:

I hope this helps. :slight_smile:

1 Like

Now i’m clicking on higher and lower buttons, the function is going well but the number "500’ is not going higher or lower.
For the text object; at first i named it Guess. Now here is the screen too and you can see that there is an script error right below the screen shot in “Yellow” color for number 43,16.
:disappointed:

Hello @Sabeen_Moin,

  • The error you mention is actually a warning. You are using a more recent version of Unity than the course material was written in. The Application.LoadLevel() method has been deprecated and replaced with SceneManager.LoadScene, however, this needs a using UnityEngine.SceneManagement; directive at the top of the class to work.

    For now, as it is only a warning and isn’t stopping your code from running I wouldn’t worry too much, when you get to Laser Defender that may be a good time to implement the newer method of loading the scenes, this is covered in the additional resources in the section, however, if you want some help, just post and tag me (@rob) etc…

  • Regarding the guess not changing from the value 500, this because you have changed your hard coded value of “” (nothing) in the NextGuess() method, to “500”. What is effectively happening is that every time you called NextGuess() from any of the other methods, it says “Ok, I need to set that text to 500”. What you want / need there is the calculated value of the wizards guess.

So, in this case;

public void NextGuess() {
	guess = (max + min) / 2;
	text.text = guess.ToString();  // note, we are using the variable which will change rather than a value that wont 
	maxGuessesAllowed = maxGuessesAllowed -1;
	if (maxGuessesAllowed <= 0) {
		Application.LoadLevel("Win");
	} 
}
1 Like

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

public class NumberWizard : MonoBehaviour {

int max;
int min;
int guess;

public int maxGuessesAllowed = 5;
public Text text;

void start () {
	StartGame();
}

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

	max = max + 1;
}

public void GuessLower () {
	max = guess;
	NextGuess();

}

public void GuessHigher() {
	min = guess;
	NextGuess();

}

public void NextGuess() {
	guess = (max + min) / 2;
	text.text = guess.ToString(); 
	//note, we are using the variable which will change rather than a value that wont 
	maxGuessesAllowed = maxGuessesAllowed -1;
	if (maxGuessesAllowed <= 0) {
		SceneManager.LoadScene("Win");
	}
}

}

Now the number is directly falling to zero when i click on higher/lower . :confused:

I have a theory, but I think it would be better to do some debugging, as this will help you to develop those skills as you move forward with the course anyway.

So, lets add a Debug.Log() statement to each of your methods (you can remove them later), so that we output some information to the console, then, when you run your game and click the higher button, we can see what is happening, and importantly, the order in which it is happening.

I’ve popped them into your script file here for you for convenience, you can simply copy and paste them into your existing file in your game etc;

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

public class NumberWizard : MonoBehaviour {

    int max;
    int min;
    int guess;

    public int maxGuessesAllowed = 5;
    public Text text;

    void start () {

        Debug.Log("Game Starting");

        StartGame();
    }

    void StartGame () {

        Debug.Log("Initialising Variables");

        max = 1000;
        min = 1;
        guess = 500; 

        max = max + 1;
	
        Debug.Log("Max : " + max);
        Debug.Log("Min : " + min);
        Debug.Log("Guess : " + guess);	
    }

    public void GuessLower () {

        max = guess;

        Debug.Log("Guessing Lower");
        Debug.Log("Max : " + max);
        Debug.Log("Guess : " + guess);

        NextGuess();
    }

    public void GuessHigher() {

        min = guess;

        Debug.Log("Guessing Higher");
        Debug.Log("Min : " + min);
        Debug.Log("Guess : " + guess);

        NextGuess();

    }

    public void NextGuess() {

        guess = (max + min) / 2;

        Debug.Log("Next Guess");
        Debug.Log("Max : " + max);
        Debug.Log("Min : " + min);
        Debug.Log("Guess : " + guess);

        text.text = guess.ToString(); 
        //note, we are using the variable which will change rather than a value that wont 
        maxGuessesAllowed = maxGuessesAllowed -1;
        if (maxGuessesAllowed <= 0) {
	        SceneManager.LoadScene("Win");
        }
    }
}

So, with this in place, run the game, click on one of the buttons, lets start with higher, and then stop the game. View the Console and you will have a little list of the statements generated from our code, take a screenshot and pop it up here, lets see what values are printed out at which state of the game.

1 Like

I finally finished this project :grin: . Thank you so much :slight_smile: .

1 Like

Hi @Sabeen_Moin,

It’s really nice to hear you have managed to locate the issue, hopefully the debugging exercise was of use and will serve you well moving forward as a way to track down issues.

The next section is Laser Defender which builds upon what you’ve already learnt and you get to make a much more interactive game - have fun! :slight_smile:

1 Like

Privacy & Terms