Answers for "unity button onclick gameobject"

C#
5

how to code button click unity

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ClickExample : MonoBehaviour {
	public Button yourButton;	
    
    void Start () {
		Button btn = yourButton.GetComponent<Button>();
		btn.onClick.AddListener(TaskOnClick);
	}	
    void TaskOnClick(){
		Debug.Log ("You have clicked the button!");
	}
}
Posted by: Guest on April-02-2020
2

unity click on gameobject

using UnityEngine;

//The gameObject needs to have a collider (if your collider is a trigger, you need to put Physics.queriesHitTriggers = true; in the start function or Physics2D.queriesHitTriggers = true; if you work in 2D)

public class nameOfClass : MonoBehaviour
{

	bool isObjectClicked;
    
	void OnMouseDown() //Detects when you click the gameObject that contains this script
    {
    	isObjectClicked = true;
    	//Your script here
    }
    
    void OnMouseUp() //Detects when you stop clicking on the gameObject that contains this script
    {
    	isObjectClicked = false;
        //Your script here
    }
}
Posted by: Guest on January-16-2021
0

unity onclick object

void Update () {
     
 }
  void OnMouseDown(){
         // this object was clicked - do something
     Destroy (this.gameObject);
  }
Posted by: Guest on June-20-2020

C# Answers by Framework

Browse Popular Code Answers by Language