Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to make a onclick event for a button

Discussion in 'Scripting' started by Storm-ace, Mar 23, 2016.

  1. Storm-ace

    Storm-ace

    Joined:
    Sep 3, 2015
    Posts:
    13
    Hey guys i'm working on a onclick lose health event. I want to make something like pokemon. When you get in combat that a GUI will show up, with all the buttons and display you need to fight the monster. Sadly i havent found any good explanation on the internet yet.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Enermy : MonoBehaviour
    5. {
    6.  
    7.     public int health = 10;
    8.     public int attackDamage = 2;
    9.     public float cooldown = 1;
    10.  
    11.  
    12.  
    13.     void OnClick()
    14.     {
    15.         if(Input.GetMouseButtonDown(0))
    16.             if (cooldown < 1)
    17.         health -= attackDamage;
    18.         cooldown = 1;
    19.     }
    20. }
     
    Last edited: Mar 28, 2016
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Get rid of the first if statement in OnClick and make sure you have a collider attached to the object. Also, add a timer in update that reduces the cooldown.
     
  3. Storm-ace

    Storm-ace

    Joined:
    Sep 3, 2015
    Posts:
    13
    Well i wanna make it for a click base in a ui. So with buttons.
     
  4. Storm-ace

    Storm-ace

    Joined:
    Sep 3, 2015
    Posts:
    13
    Burp those somebody know a lesson in making buttons with scripts without load.level but more custom stuff?
     
  5. matta1989

    matta1989

    Joined:
    May 20, 2013
    Posts:
    27
    What is it you are trying to achieve here?

    You want to make a button which does something?


    Code (CSharp):
    1. public class button : MonoBehaviour
    2. {
    3.  
    4. public Button myButton;
    5.  
    6. void Awake()
    7. {
    8. myButton = GetComponentInChildren<Button>();
    9. }
    10.  
    11. void Start()
    12. {
    13. myButton.onClick.AddListener(() => myFunction());
    14. }
    15.  
    16. public static void myFunction()
    17. {
    18. Debug.Log("Hello, im a button");
    19. }
    20.  
    21. }
    22.  
    something like this ?

    Keep in mind this implies that the script above is attached to the canvas which contains your button.

    if the script is attached to another game object you need to use the
    Code (CSharp):
    1. myButton = GameObject.Find("myButton").GetComponent<Player>();
    if the GameObject is called myButton that is, if not just replace it with said gameobject name that holds the button script