How to make the higher and lower buttons unclickable if the guess= 1000?

10_ui_cud
2d
#1

hello …

i want to make the buttons higher or lower unclickable if the guess = max or 1000 in our case here or if the guess is equal to min or 1 so that only correct button remains clickable ?
i know this problem can be solved a number of ways and probably in easier ways than this but i guess this way is more fun and learning it can be useful for future situations as well

this is my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class number : 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()
    {
               NextGuess();
    }

    public void OnpressHigher ()
    {
        min = guess+1;
        NextGuess();
    }

    public void OnpressLower ()
    {
        max = guess -1;
        NextGuess();
    }

    void NextGuess()
    {
        guess = Random.Range(min, max);
        guessText.text = guess.ToString(); 
    }
  }
#2

Hi Mustafa,

Did you also create a thread over on Udemy?

I’ve posted an example there.

[SerializeField] Button higherButton;
[SerializeField] Button lowerButton;

void NextGuess()
{
    // If 'guess' is within range, create new guess value
    if (guess > guess || guess < 1000)
    {
        guess = Random.Range(min, max);
        guessText.text = guess.ToString();
    }

    // If 'guess' is not within range, disable buttons
    else
    {
        higherButton.interactable = false;
        lowerButton.interactable = false;
    }
}

Maybe this is not what you imagined. Feel free to rewrite the if-conditions so they match your rules.

#3

yeah.

i did this as u showed

but it gives me this error

also how do u specify in the editor that these 2 buttons are those that we need ? these 2 [SerializeField] Button higherButton;
[SerializeField] Button lowerButton;

#4

First of all, fix the first issue in your console. It probably refers to line 51.

Regarding the comparison, use >= and <= instead of => and =<.

The [SerializeField] attribute exposes a field in the Inspector. Assign your buttons to the respective fields.

#5

yes i had those problem too …it worked when i moved the if statements into the onpresshigher and onpresslower functions and put it under the nextguess(); method and removing else

this only fixed the going above 1000 or 1 but if the numbers were in between we would still get this issue but changing it to

if (min == max ||  max < min)
{
    LowerButton.interactable = false;
    HigherButton.interactable = false;
}

solved this.

added this to both buttons and i guess this bug is finally solved for good

this is my code entirely if anyone else wants to solve this issue by making the buttons unclickable

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

public class number : MonoBehaviour
{
    [SerializeField] int max = 1000;
    [SerializeField] int min = 1 ;
    [SerializeField] TextMeshProUGUI guessText;
    [SerializeField] Button HigherButton;
    [SerializeField] Button LowerButton;

    int guess; 

    // Start is called before the first frame update
    void Start()
    {
        StartGame();
    }

    void StartGame ()
    {       
        NextGuess();
    }

    public void OnpressHigher ()

    {
        min = guess + 1;
        NextGuess();

        if (  min == max || max < min)
        {
            HigherButton.interactable = false;
            LowerButton.interactable = false;
        }
    }

    public void OnpressLower ()
    {
        max = guess - 1;
        NextGuess();

        if (min == max ||  max < min)
        {
            LowerButton.interactable = false;
            HigherButton.interactable = false;
        }
    }

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

thanks alot for helping till the end you taught valuable things. im sure more people will benefit as well if they read through too.

#6

Good job, and thanks for sharing your code. I’m sure other people will benefit from it. :slight_smile:

By the way, while I was editing your post to format the code properly, I noticed that I made a stupid mistake in my if-condition. Of course, it had to say: if (guess > 1 || guess < 1000). guess == guess does not make any sense.

(min == max || max < min) could be written as (min >= max) or (max <= min). Yours is not a mistake but I thought I should mention this because you tried that in your previous code.

Keep up the good work! :slight_smile:

1 Like
closed #7

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