0

Little disclaimer: I haven't posted on here before so please let me know if I'm missing any vital information

I'm making an order taking system for a game in Unity. This system has several size buttons that, when clicked should change the corresponding boolean value in my script to the opposite value. (if it was true it'll be false and vice versa). The boolean value that is changed depends on the button name and so far I can get the button name but I have issues when I try to convert it to a boolean.

Simply, my question is, how can I use the button names to change the right boolean values?

Button Click Function

The 'bool size = bool.Parse(btnName);' line is attempting to create a temporary variable and use that variable to switch the values, but I'm not sure how to connect it to the boolean I've already created.

Size Booleans

Size Buttons in Unity

I've tried to convert the boolean in a different way (using Convert.ToBoolean and others). I've also tried passing in a parameter for the boolean but I can't find a way to pass in the right one depending on which button is pressed.

Also, I know I could have an if statement for every boolean and just compare the button name but I was trying to make it so my code isn't so repetitive if possible.

1
  • 2
    Hi, Please dont post pictures of code, its uncopyable, unsearchable. Why are you trying to convert a name of a button to true/false? Commented Aug 24, 2024 at 16:56

2 Answers 2

0

bool.Parse, Convert.ToBoolean convert string to boolean value, which means e.g. "True" of type string to true of type bool.

Your button names aren't "True" and "False", so they can't be converted to boolean.

What you are trying to do is not called "converting to boolean". You are trying to find a variable by a string name. This is formally doable via reflection, but reflection comes at a cost of performance and should not be used casually.

The solution to your problem is storing your variables in a Dictionary:

Dictionary<string, bool> _flags = new() 
{
    { "EVM", false },
    { "small", false }
    ...
};

Then, in your SizeClicked yo can do:

public void SizeClicked()
{
    var btnName = EventSystem.current.currentSelectedGameObject.name;
    _flags[btnName] = !_flags[btnName];
    ...

_flags[btnName] is how you access values stored in a dictionary by key.

  • _flags[btnName] = true; is how you set the value.
  • var value = _flags[btnName]; is how you get the value.
  • if (_flags[btnName]) { } is how you check the boolean value.

There is also a solution without dictionary. Either use if:

public void SizeClicked()
{
    var btnName = EventSystem.current.currentSelectedGameObject.name;
    if (btnName == "EVM");
        EVM = !EVM
    else if (btnName == "small")
        small = !small
    ...

or switch:

public void SizeClicked()
{
    var btnName = EventSystem.current.currentSelectedGameObject.name;
    switch (btnName)
    {
        case "EVM":
             EVM = !EVM
            break;
        case "small":
            small = !small
            break;
        ...
Sign up to request clarification or add additional context in comments.

Comments

0

Unfortunately, I didn't get to see your photos!

For button names, I think you should put a name that is understandable and related to the button, and put a 1 or 0 at the beginning or end of their name. My own suggestion is that the first name is the buttons, so I will give an example with the same method.

bool isZero = myButtun.name.StartsWith(0);

I don't know why you want to take a pulse from a button that has been saved correctly and vice versa.

I wish you could explain more about what you want to do so that we can achieve a better result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.