-3

So im trying to make a if statement with a ui button in Uinty and i need to get the name of the button for the if state ment but it tells me that it is not convertabel even thou it is bouth a string or is it not?

I tried it like this and was expecting it to work but it didn´t

public class UIGetClick : MonoBehaviour
{
public bool clicked = false;
public string ButtonName = EventSystem.current.currentSelectedGameObject.name;
public void Back()
{
    string ClickedButtonName = EventSystem.current.currentSelectedGameObject.name;
}

public void Freez()
{
    if (ButtonName = "Back")
    {
        clicked = true;
    }
    else
    {
        clicked = false;
    }
    Debug.Log(clicked);
  } 
}

Assets\UIGetClick.cs(18,13): error CS0029: Cannot implicitly convert type 'string' to 'bool'

I also tried bool. and string. but that didn´t work either.

public class UIGetClick : MonoBehaviour
{
public bool clicked = false;
public string ButtonName = EventSystem.current.currentSelectedGameObject.name;
public void Back()
{
    string ClickedButtonName = EventSystem.current.currentSelectedGameObject.name;
}

public void Freez()
{
    if (ButtonName == "Back")
    {
        clicked = true;
    }
    else
    {
        clicked = false;
    }
    Debug.Log(clicked);
}

NullReferenceException: Object reference not set to an instance of an object UIGetClick..ctor () (at Assets/UIGetClick.cs:9)

any thoughs and help?

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • I think you need something like ButtonName.Text (or something like that for the value of the button text). – Brad Nov 07 '22 at 17:59
  • Please re-read the [mre] guidance on posting code to improve your future questions. In particular for the first error you needed to show just one line of code `if (ButtonName = "Back"){}` (it is unclear why you did not understand the error as the second copy of the same line in the question shows correct comparison)... For the NRE you should also show just one line (not sure which one as you did not say) and clarify what steps you too to debug after reading classical "C# What is NRE and how to fix it". – Alexei Levenkov Nov 07 '22 at 18:17
  • duplicate [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Nov 07 '22 at 19:05
  • The compiler reads your if statement like this `if(ButtonName){...}` because of your typo `=` not being `==`. – Willem Nov 07 '22 at 22:38

2 Answers2

1

This is an assignment and returns a string with the same value

ButtonName = "Back"

which is why you can do things like

var x = ButtonName = "Back";

which ends up with both x and ButtonName be "Back"

while this would be a valid bool check

ButtonName == "Back"

In general though

string ButtonName = EventSystem.current.currentSelectedGameObject.name;

needs to go into a method in the first place .. it makes not much sense an class field declaration during the instantiation of this type. You rather need to gather this information the moment you perform your check

public class UIGetClick : MonoBehaviour
{
    public bool clicked = false;
    public string ButtonName;

    public void Back()
    {
        ButtonName = EventSystem.current.currentSelectedGameObject.name;
    }

    public void Freez()
    {
        clicked = ButtonName == "Back";
       
        Debug.Log(clicked);
    } 
}

However, tbh I don't completely understand what this is supposed to do. Since if this anyway is only called by a specific button (the back button I suppose) then why the name check?

derHugo
  • 83,094
  • 9
  • 75
  • 115
1

The error is telling you that you are trying to use a string as if it was a bool.

Change ButtonName = "Back" to ButtonName == "Back".

One equal sign (=) is for assignment. ButtonName = "Back" means Change ButtonName to "Back"

Two equal signs (==) are for checking for equality. ButtonName == "Back" means Is ButtonName considered equal to "Back"?

Petrusion
  • 940
  • 4
  • 11