Home > Mobile >  Button OnClick() not detecting public method
Button OnClick() not detecting public method

Time:06-20

I have looked at other answers, but it hasnt helped because I did add the object and not the script in the OnClick() box, but it still won't let me access the variable. What's going on?

The object in the box, not the script

The Method I want to access

public void IncreaseStat(string stat, float val)
{
    switch (stat)
    {
        case "Hunger":
            gm.juice.hunger  = val;
            break;
        case "Energy":
            gm.juice.energy  = val;
            break;
        case "Happiness":
            gm.juice.happiness  = val;
            break;
        case "Health":
            gm.juice.health  = val;
            break;
        default:
            Debug.LogError("INVALID STAT: "   stat);
            break;
    }
}

CodePudding user response:

Look like Unity now allow you to put function with more than one parameter into the OnClick() event

using UnityEngine;
using UnityEngine.UI;

public class ButtonEventDemo : MonoBehaviour
{
    public void FunctionWithNoParams()
    {
        // This is working fine
    }

    public void FunctionWithStringParam(string text)
    {
        // This is working fine
    }

    public void FunctionWithIntParam(int number)
    {
        // This is working fine
    }

    public void FunctionWithButtonParam(Button button)
    {
        // This is working fine
    }

    public void FunctionWithTwoParams(int first, int second)
    {
        // Cannot put this function into OnClick() event
    }
}

OnClick() demo

  • Related