Issue with on.click unity UI buttons

Hey there, me and my friend are making a little game.

We wanted to change some values when we press on a UI button. but we came to a dead end. We have tried a lot of things but we couldn’t figure it out. we have tried to find the issue by using debug logs but we never gotten 1 back in the console. so i was thinking, the buttons aren’t calling the piece of script that they should be calling.

idk if it works but here is a screenshot of how the button looks like (i did drag the game object to the onClick())

also the code:

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class controlScript : MonoBehaviour
{
    public static float controlDamage;
    public static float controlAttackSpeed;
    public static float controlGruntSpawnRate;

    public static bool easyMode = false;
    public static bool mediumMode = false;
    public static bool hardMode = false;

    public Button easyButton;
    public Button mediumButton;
    public Button hardButton;


    // Start is called before the first frame update
    void Start()
    {
        easyButton.onClick.AddListener(easyModeSettings);
        mediumButton.onClick.AddListener(mediumModeSettings);
        hardButton.onClick.AddListener(hardModeSettings);

        controlDamage = 0.5f; //make damage lower for less damage, higher for more
        controlAttackSpeed = 2f; // make attack speed higher for slower attacks, lower for faster attacks
        controlGruntSpawnRate = 1f; //make higher to spawn less grunts, lower to spawn more

    }

    // Update is called once per frame
    void Update()
    {
        //Debug.Log(controlDamage);
    }

    public void easyModeSettings()
    {
        easyMode = true;
        mediumMode = false;
        hardMode = false;

        settingsChecker();
    }

    public void mediumModeSettings()
    {
        easyMode = false;
        mediumMode = true;
        hardMode = false;

        settingsChecker();
    }

    public void hardModeSettings()
    {
        easyMode = false;
        mediumMode = false;
        hardMode = true;

        settingsChecker();
    }

    private void settingsChecker()
    {
        Debug.Log("settingscheck");
        if (easyMode == true)
        {
            controlDamage = 0.5f; //make damage lower for less damage, higher for more
            controlAttackSpeed = 2f; // make attack speed higher for slower attacks, lower for faster attacks
            controlGruntSpawnRate = 1f; //make higher to spawn less grunts, lower to spawn more

            Debug.Log("eMode");

            easyMode = false;
            mediumMode = false;
            hardMode = false;
        }
        else if (mediumMode == true)
        {
            controlDamage = 0.8f; //make damage lower for less damage, higher for more
            controlAttackSpeed = 1.2f; // make attack speed higher for slower attacks, lower for faster attacks
            controlGruntSpawnRate = 0.6f; //make higher to spawn less grunts, lower to spawn more

            Debug.Log("mMode");

            easyMode = false;
            mediumMode = false;
            hardMode = false;
        }
        else if (hardMode == true)
        {
            controlDamage = 1f; //make damage lower for less damage, higher for more
            controlAttackSpeed = 0.9f; // make attack speed higher for slower attacks, lower for faster attacks
            controlGruntSpawnRate = 0.4f; //make higher to spawn less grunts, lower to spawn more

            Debug.Log("hMode");

            easyMode = false;
            mediumMode = false;
            hardMode = false;
        }
    }
}

Most of the time this due to 3 main culprits :

  • You probably have something invisible on top the image that has “Raycast Target” enabled
  • The button doesn’t have an image under it that has “Raycast Target” enabled
  • You don’t have a “Graphics raycaster” on the canvas

In either cases, what you can do is select your EventSystem in the scene and put the cursor on top of the button , it should tell you the first object the cursor in pointing at like so

You can try wrapping it inside of a delegate.

easyButton.onClick.AddListener( delegate{easyModeSettings();} );
mediumButton.onClick.AddListener( delegate{mediumModeSettings();} );
hardButton.onClick.AddListener( delegate{hardModeSettings();} );