Number Wizard UI: ToString issue

Hello!

I am on the Number Wizard UI course section #42 “Using ToString()”

I have done something that I cannot figure out. My [SerializeField] TextMeshProUGUI guessText; values within Unity show MAX 1000 and MIN 1. When I went over Rick’s code everything is identical. I push play and the value displayed in my Guess Text field is showing 00. If I changed the value for MAX to 2000 it then will display 1000 on play.

What the hell am I missing lol. This will be simple I am sure. Are we permitted to paste our code here?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class NumberWizard : MonoBehaviour

{

    [SerializeField] int max;
    [SerializeField] int min;
    [SerializeField] TextMeshProUGUI guessText;

    int guess;

// Start is called before the first frame update

void Start() 
{
   StartGame();
}

void StartGame()
{
    guess = (max + min) / 2;
    guessText.text = guess.ToString();
    max = max + 1;
}

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

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

void NextGuess()
{
    guess = (max + min) / 2;
}

}

Well I figured it out. This code I quoted. I had to remove the () around max + min to resolve the issue.

Spoke too soon. Didn’t realize the line isnt dividing by 2. It now shows 1000 but we want 500 to show.

Hi yinzang,

Welcome to our community! :slight_smile:

If I changed the value for MAX to 2000 it then will display 1000 on play.

Take a look at your StartGame method. Your code displays the guess value, not the max value. And the guess value is calculated and assigned with guess = (max + min) / 2;. For this reason, it is expected that the guess value at the beginning of your game is 1000 if max = 2000.

I push play and the value displayed in my Guess Text field is showing 00.

I’m not sure if I understood this correctly but the guess text field is supposed to display 1000. If it doesn’t, maybe the field is just too small. Adjust its size in the scene window.

Hi Nina!

The value that is supposed to display on start is 500 but is showing 00. Max value is set to 1000 and min is set to 1. In the guess = (max + min) / 2; should get me there I thought but its displaying 00

Could you please share screenshots of what you see in Unity?

If the display shows “00” instead of “500”, the 5 might just be cropped. Tht’s why I suggested to check the bounding box of the guess text field in the scene window.

Here they are. Thanks for helping, Nina!

Thank you for the screenshots. That’s indeed interesting. Instead of “???”, try to type “500”. I’m wondering if the problem is caused by the font. If “500” gets displayed in the scene and game window, the problem is somewhere else.

Also log the value of guess into your console. Maybe also the value of guess.ToString() to see what string gets assigned to guessText.text during runtime.

Ok I will try that right after work in a few hours. Thanks!

Wow that is embarrassing… The font I downloaded just happened to have everything in it but the #5. The code was always working… Just I failed and downloaded a font with a flaw. Sorry for wasting your time but I really do appreciate the help!

No worries. You didn’t waste my time at all. Missing characters is fairly common in free fonts but, usually, if they have numbers, all numbers are there.

Are you sure that the font itself is missing the 5? Open the font file. If the 5 is included in the font, the glpyh might just be missing in the TextMeshPro font atlas. In that case, you could try to regenerate the atlas with different settings. If you need help, a screenshot of the current settings would be helpful, so I can assist you.

I think you are right. My atlas didn’t generate all the characters. Thanks Nina!!!

Completed the project: Udemy Course: Number Wizard by yinzang

Onto block breaker :smiley:

Good job on solving the problem! :slight_smile:

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

Privacy & Terms