Help I am getting an error CS0079 in my code and I don't know why.

I am trying to make some flexible UI that creates a set number of buttons based on a list. I have done this before and had no problems. However, when I try to do it again using almost the exact same code I am getting a strange error.

The code I used the first time.

foreach (ChoiceButton choiceButton in buttons)
    {
      // Instantiate button
      GameObject newButton = Instantiate(choiceButtonPrefab, choiceButtonPosStart, Quaternion.identity, choiceButtonParent.transform);

      // Set button position
      Vector3 choiceButtonPos = choiceButtonPosStart;
      choiceButtonPos.x += _x * choiceButtonPosOffsetX;
      newButton.transform.localPosition = choiceButtonPos;
      _x++;

      // Set button text
      newButton.GetComponentInChildren<TextMeshProUGUI>().text = choiceButton.label;

      // Add button event listener
      newButton.GetComponent<Button>().onClick.AddListener(() => onClickChoice(choiceButton.newSceneID));
    } 

The code giving me the error.

foreach(string projectPath in projectPaths)
    {
      // Instantiate button
      GameObject projectButton = Instantiate(projectButtonsPreFab, projectButtonsPosStart, Quaternion.identity, projectButtonsParent.transform);

      // Set button position
      Vector3 projectButtonsPos = projectButtonsPosStart;
      projectButtonsPos.y += y * projectButtonsPosOffsetY;
      projectButton.transform.localPosition = projectButtonsPos;
      y++;

      // Set button text
      string buttonText = projectPath.Remove(0,16);
      projectButton.GetComponentInChildren<TextMeshProUGUI>().text = buttonText;

      //Add button event listener
      projectButton.GetComponent<Button>().onClick.AddListener(() => UpdateUISceneButtons(projectPath));
    }

For some reason the last line of the code

projectButton.GetComponent<Button>().onClick.AddListener(() => UpdateUISceneButtons(projectPath));

is giving me this error.

The event ‘Button.onClick’ can only
appear on the left hand side of += or
-=

And yet I did almost the exact same thing in the first script and it had no problem with it. So why is it throwing an error now?

I have been trying to find the answer online and yet can’t find anything.

Any help would be much appreciated.

Check your imports. My guess is that in your first script you are using IMGUI – that is, you have this line of code in your script:

using UnityEngine.UI;

and in the second one you are using UIElements – that is, you have this line of code in your script instead of the one above:

using UnityEngine.UIElements;

The reason for the compilation errors is that onClick is a different type for IMGUI buttons vs UIElements buttons. UnityEngine.UI.Button uses onClick.AddListener(() => YourFunction()) like you have, but UnityEngine.UIElements.Button used to use the C# event style onClick += YourFunction – actually though, that property is obsolete on UIElements buttons, and you should use clicked += YourFunction instead. Hope this helps!

HI! I was having this same problem, there are zero good answers out there.
Heres the anwer tho. :+1:

projectButton.GetComponent<Button>().clicked += delegate { UpdateUISceneButtons(projectPath); };