About 'Using ToString()'!

In this video (objectives)…

  1. Hook up our buttons to the previously created methods.
  2. Link our UI text field to our code.
  3. Use ToString() to convert our integer into a string to be displayed in our text field.

After watching (learning outcomes)… Connect a variable in our code with a text field in our UI.

(Unique Video Reference: 9_UI_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

Hello Rick,
There is an issue in the Random Range Lesson. Since there is no discussion for that lesson I figured I would post it here. When we use the following code:
min = guess +1;
max = guess -1;
to prevent the computer from guessing the same number we break the game. It makes it possible for the player to get a number higher than 1000 if they keep hitting “Higher” because the min becomes greater than the max. I know that the player should have already hit “Success” at this point but I feel we can prevent this somehow. Is there an easier way outside of adding “if functions”?

Hey James,

Sorry about the lack of forum post - there should be one against that lecture now.

You can use Mathf.Clamp() to constrain the range of the values. Here is the change we can make to our NextGuess() method…

void NextGuess()
{
    guess = Mathf.Clamp((Random.Range(min, max + 1)), min, max);
    guessText.text = guess.ToString();
}

Essentially, we wrap our Random.Range in the Mathf.Clamp and pass in 3 values - the guess (from min to max), the lowest possible value which will be our min, the highest possible value which will be our max).

Hope this helps.
Rick

Nope it still happens. The math clamp keeps the guess between the min and max but our code is already doing that by itself. The problem comes because we are increasing the min and decreasing the max. So once we get to the point where we increase the minimum above 1000 our guess will split out a number above 1000 even with the math clamp thing. Same thing if we go down to 1, we can get negative numbers. I’m going to play around with it while I wait for the next lessons to be updated and I’ll let you know what I come up with. Thank you for the lessons and your quick responses!

I ended up just adding the If function to make sure the code wont run once the min or max values are outside our range we want.

public void OnPressHigher()
{
min = guess + 1;
if (min <= 1000 && max >= 1)
{
guessText = “Barnacles! I was too low. I’ll get it this time!”;
GuessWork();
}
}

1 Like

Nice job!

1 Like

I was going to report this issue too, but it looks like you got to it first. We can clean up the hard-coded values with a slight change.

In effect this forces the code from ever getting outside either the starting min and max or any subsequent one. Eventually the player is forced to use the button indicating the Math Wizard program guessed the right number if he or she wants to make any change.

Here is how it looks in my code:

public void OnPressHigher()
{
    //This keeps min from getting more than max
    if (min >= max)
    {
        return;
    }
    else
    {
        min = guess + 1;
        NextGuess();
    }
}

public void OnPressLower()
{
    //This keeps max from getting less than min
    if (max <= min)
    {
        return;
    }
    else
    {
        max = guess - 1;
        NextGuess();
    }
}


1 Like

I had a go at the challenge and added the “guessText.text = guess.ToString()” call in the Update method (and it works ok).

Is there any reason why you chose to add it to the “Nextguess” method instead?

I’m getting an error when setting up the guess text…
NullReferenceException: Object reference not set to an instance of an object
NumberWizard.StartGame () (at Assets/Scripts/NumberWizard.cs:23)
NumberWizard.Start () (at Assets/Scripts/NumberWizard.cs:17)



Thanks for any insight! Cheers!

1 Like

Hey everybody,

Is this the right place to ask questions about the chapter? If not, I am very sorry - asked the same thing on udemy but there doesnt seem to be much going on in the discussions.

I am currently working through the number wizard ui part, and just finished the session on using .toString to make our guess appear on the screen. Everything works, but i just have some small questions for understanding:

  1. Rick in the video initialized a textmeshpro text variable, named “guessText”. However, the name of the variable is different in the inspector (“Guess Text”)? Why is that, and is there a rule to that to avoid confusion?

  2. Our NextGuess function changes our guess, and then prints it changes the text in the textmeshpro gameobject. Now, at first I thought this would not work, since NextGuess is a void function (no return value) and feeding some value into the text object seems to contradict that. Yet, it works just fine. Can somebody give some pointers on what the void characteristic actually means in these contexts, and what I can and cannot do in/with them?

Thanks a lot!

i have the same issue here, been trying to figure what i did wrong, but i followed exactly the same as video.

Hi Bing, welcome to the community :slight_smile:

It would be a guess (excuse the pun) but I would imagine you haven’t created the reference between the UI Text GameObject and the exposed guessText field of the NumberWizard script in the Inspector.

Select the GameObject in the Hierarchy that you have added this script to and check, if it states none then you need to drag across the UI Text GameObject that you want to output the guess to into that field.

Hope this helps :slight_smile:

[SOLVED] did exactly as in the video but i get this compiler error?

Assets\Scripts\NumberWizard.cs(24,30): error CS1061: ‘int’ does not contain a definition for ‘Tostring’ and no accessible extension method ‘Tostring’ accepting a first argument of type ‘int’ could be found (are you missing a using directive or an assembly reference?)

wierd? looks like tostring isnt accepted?

found it i had used a s instead of S in ToString , as im dislectic i had to reread about a thousand times to find the cullprit :wink:

Privacy & Terms