- Home /
How to detect multiple button clicks in one script.
So I've been searching for literally hours (accumulated over weeks of research after giving up several times) how to detect multiple button clicks in a single script vs individual scripts on a per game object basis.
I'm trying to be as efficient as possible when writing my scripts and so at the moment I'm wanting to use the OnMouseDown function with multiple buttons on my games menu, but right now I gave them each a separate script similar to this:
function OnMouseDown() {
gameController.speed = 2;
Application.LoadLevel(2);
}
The buttons sets the games difficulty, but I want to use a script that can consolidate everything instead of making a new script per each button. Is this possible? I've seen something similar in C# using enumerations but I haven't been able to produce the same result using UnityScript. I hope my question isn't confusing, just had my morning coffee haha.
How would I know which button it's currently colliding with?
Is this keyboard keys / mouse buttons you're talking about or in-game buttons?
In game buttons using the new tools in Unity 4.6.
Thank you both so much, i was almost forced to use the On$$anonymous$$ouseUpAsButton() function for an upgrade system.. which seems to be singular to the button its attached to. I will be trying this out now as there is about 8 or so xD.
Answer by Mmmpies · Jan 05, 2015 at 07:57 PM
OK I added 3 buttons to a canvas and renamed them Button1, Button2 and Button3 (I know, imagination is my strong point).
They just sit on a background Image. Then I wrote this:
using UnityEngine;
using System.Collections;
public class Buttons : MonoBehaviour {
public void ButtonClicked(RectTransform rtClicked)
{
Debug.Log ("The button clicked was " + rtClicked.name);
}
}
And attached that to the background, but that could be anywhere, like on the canvas or an empty gameObject.
Then in each Buttons "On Click ()" I clicked the + symbol and dragged the background Image (the thing with the script on it) onto the slot.
From the dropdown I picked Buttons -> ButtonClicked
Because it's expecting a rectTransform a new slot appears in the inspector and I dragged the relevant button onto it.
The upshot is I can click on each button and the one script knows which one was clicked.
Is that the kind of thing you're after?
This is definitely in line with what I am thinking, I forget the UI buttons have the new "On Click()" options, I will implement this and let you know what kind of results I get.
Yep buttons come with them already built in but you can add an event to any UI element and do the same.
Hope it works.
Woah, thanks to this, i manage to reduce the lines of codes in our game. Definitely a linebreaker. LOL :) :P
Answer by Kamuiyshirou · Jan 05, 2015 at 08:09 PM
Input.GetMouseButtonDown returns true whenever the mouse button is down, regardless of what is under the mouse pointer.
You can raycast from the mouse screen position and see if it hits the object in question. See http://answers.unity3d.com/questions/28064/get-mouse-position-on-object.html for information about how to get the object under the mouse cursor.
Hope this helps :)
Your answer
Follow this Question
Related Questions
Make gameobject appear on collision 1 Answer
AoTTG - Modding with Visual Basics + .NetReflector SFX Mod 0 Answers
continous movement for rigidbody2d using addforce with buttons 2 Answers
Cannot convert 'UnityEngine.GameObject[]' to 'UnityEngine.GameObject'. 2 Answers
hi, i dont understand this error. 2 Answers