Trying to post remaining guesses to UI

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

public class NumberWizard : MonoBehaviour {



// Use this for initialization
int max;
int min;
int guess;
int maxGuessesAllowed;

public Text text;
public Text guessesLeft;

void Start () {
	StartGame ();

}

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

void NextGuess () {
	guess = (max + min) / 2;
	maxGuessesAllowed = maxGuessesAllowed - 1;
	text.text = guess.ToString();
	guessesLeft.text = maxGuessesAllowed.ToString();


		if(maxGuessesAllowed<=0){
			Application.LoadLevel("Win");
	}
}

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

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

guessesLeft.text = maxGuessesAllowed.ToString(); returns a NullReferenceException when I try to use it. I’ve basically copied the functionality of the text.text object that updates the current computer guess. text.text works fine, but my guessesLeft.text throws the nullref.

Any idea what I’m doing wrong?

1 Like

Hi @lol2dubs, whilst you have added the public Text guessesLeft; declaration at the top of your script, have you added the guessesLeft Text object to this exposed variable in the Inspector?

Also, not that it is part of the problem you are experiencing, but for info only, whilst this is great for readability when you are starting out, you can abbreviate it:
maxGuessesAllowed = maxGuessesAllowed - 1;

to this:
maxGuessesAllowed -= 1;

or, even shorter still:
maxGuessesAllowed--;

The – operator (C# reference), you will see on the left hand navigation a list of lots of others that may prove useful also.

Hope this is of use.

3 Likes

My Unity setup mirrors the working Guess text that changes the computer’s guess in game from the default of 500 to whatever the result of the division /2 is.

That UI Text GameObject in Unity doesn’t have an assignment on it. I thought the same thing and am frankly confused as to how the text.text code is changing the computer Guess in game.

BLAH.

I see what you mean now. BLAH.

This was my problem.

It’s working now that I added the GuessesLeft UI Text to the script in the Inspector. I forgot this step.

Thanks for pointing me in the right direction!

1 Like

I shall stop replying then - and you are welcome :slight_smile:

To explain the text.text…

text is the name of the variable of type Text. That is the Type of the object you are passing in by reference in the Inspector (when you drag/drop etc).

text.text is your variable text, which has a property called text. This is the actual textual content of that object. (the words/score/guess)

It may be easier to read/understand if you were to change:
public Text text;

to
public Text wizardsGuess;

Does this make a little more sense now?

In your NextGuess() method you would then have;

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

So in this method you are saying, set the text property of the wizardsGuess variable (this is the Text object you have references via the inspector) to equal the next guess that has been calculated.

Again, hope this helps :slight_smile:

It does! Thank you again!

1 Like

Great - looking forward to seeing you share a link to your Number Wizard game in the Showcase forum in due course :slight_smile:

I actually tried to share the Text Adventure game the other night, but the gamebucket.io wouldn’t load for me. I’m hoping it’ll load when I’m done with this one. :slight_smile:

EDIT: gamebucket.io is loading fine for me currently. Maybe maintenance.

Oooh, there were some Goblins in the Data Centre the other day, most likely the problem. Should you encounter any further issues, there is also itch.io which offers the same kind of functionality. :slight_smile:

You rock.

Thanks so much for all the info!

You wouldn’t happen to be on Steam would you?

1 Like

You’re welcome.

I am yes, although don’t play that often these days.

1 Like

I wouldn’t mind having another friend who is easy to reach.

Add me to Steam, if you wouldn’t mind…

Invite sent :slight_smile:

Thanks again.

1 Like

Privacy & Terms