- Home /
Question by
nomischen · May 15, 2016 at 01:25 PM ·
uibuttonmouseclick
How to click a UI button without activating mouse
I made a UI button to select a block and my mouse click spawns a block, but I don't want it spawn a block when I'm about to press a button, and it does anyway..
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class GameData : MonoBehaviour {
public Text crateButton;
public Text crateButtonP;
public Text metalButton;
public Text metalButtonP;
public GameObject crate;
public GameObject crateP;
public GameObject metal;
public GameObject metalP;
float blockType = 2;
float crateAmount = 100;
float blockAmount = 100;
float crateAmountP = 100;
float blockAmountP = 100;
void Start (){
crateButton.text = crateAmount.ToString ();
metalButton.text = blockAmount.ToString ();
crateButtonP.text = crateAmountP.ToString ();
metalButtonP.text = blockAmountP.ToString ();
}
void Update () {
if (Input.GetMouseButtonDown(0)) {
Vector2 position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (blockType == 1){
if (crateAmount >= 1){
Instantiate (crate, position, Quaternion.identity);
crateAmount = crateAmount - 1;
crateButton.text = crateAmount.ToString ();
}
}
if (blockType == 2){
if (blockAmount >= 1){
Instantiate (metal, position, Quaternion.identity);
blockAmount = blockAmount - 1;
metalButton.text = blockAmount.ToString ();
}
}
if (blockType == 3){
if (crateAmountP >= 1){
Instantiate (crateP, position, Quaternion.identity);
crateAmountP = crateAmountP - 1;
crateButtonP.text = crateAmountP.ToString ();
}
}
if (blockType == 4){
if (blockAmountP >= 1){
Instantiate (metalP, position, Quaternion.identity);
blockAmountP = blockAmountP - 1;
metalButtonP.text = blockAmountP.ToString ();
}
}
}
}
public void CreateBox (){
blockType = 1;
}
public void CreateMetal (){
blockType = 2;
}
public void CreateBoxP (){
blockType = 3;
}
public void CreateMetalP (){
blockType = 4;
}
}
Comment