Scripts and buttons - problem

Hi, I am doing some health icons on the screen as a canvas element. I’ve made a button and made a script for it. When the Player will find a FirstAid the button appear on the screen. Now, when his health is low, I press the FirstAid icon and the health should add extra points. But it doesn’t work. Can you tell me what is wrong in the code, or maybe I need to select appropriate function i button OnClick?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HealthInjection : MonoBehaviour {

    PlayerController playerScript;
    GameObject Player;
    public GameObject firstaid;

    void Awake () {
        Player = GameObject.Find ("Player");
        playerScript = Player.GetComponent<PlayerController> ();
    }

    void OnClick()
    {
        playerScript.health += 100.0f;
        firstaid.SetActive (false);
    }

    void Update () {
    if (playerScript.health > 100) playerScript.health = 100;
}
    private GameObject player;
}

Is your OnClick hooked up to your button in the inspector?

Also not a big fan of your update. Unless you have some weird situation for regen, you should simply do that check any time the player can gain health instead of every frame. Just some advice.

Yes it is, but I don’t know which function should I use. I take a script and shift it into the OnClick function element, but there is only:
bool enabled
string name
bool runInEditMode
string tag
bool useGUILayout
CancelInvoke
SendMessage
StopAllCoroutines
StopCoroutine

Oh, I’m stupid :smile: it needed only to write “public” before void OnClick() :smile: