Problem with Number Wizard

When clicking Higher or Lower, my number disappears, or just turns black.

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

public class NumberWizard : MonoBehaviour {

int max;
int min;
int guess;
int maxGuesses = 10;

public Text text;

// Use this for initialization
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();
}

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

}

not sure what else it could be. The buttons are linked up right. I think.

Hello @QwikSRT8,

If you look at your NextGuess() method you will see on the second line you are setting the text property of the text variable to “” (e.g. nothing).

The text variable is the one referencing your Text UI object, so you are effectively clearing the text everytime you call NextGuess().

NextGuess() is called each time you click on the higher or lower buttons, calling the GuessHigher() or GuessLower() methods respectively.


See also;

User Forum Guides : How to apply code formatting within your post

Yeah, I didn’t pay attention. I forgot to add my guess.Tostring()

1 Like

I appreciate the feedback though!

1 Like

You’re more than welcome. I’m glad you can move forwards with the course again :slight_smile:

Privacy & Terms