Home > front end >  Cant Fix my if statement with button name
Cant Fix my if statement with button name

Time:11-08

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?

CodePudding user response:

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"?

CodePudding user response:

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?

  • Related