Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by adrianrodriguez · Jan 02, 2015 at 06:24 PM · javascriptgameobjectbuttonsclick objects

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adrianrodriguez · Jan 02, 2015 at 02:31 PM 0
Share

How would I know which button it's currently colliding with?

avatar image adrianrodriguez · Jan 05, 2015 at 07:15 PM 0
Share

Yes I did, was completely confused by it. :)

avatar image Steampunk_Droid · Jan 05, 2015 at 07:24 PM 0
Share

Is this keyboard keys / mouse buttons you're talking about or in-game buttons?

avatar image adrianrodriguez · Jan 05, 2015 at 07:29 PM 0
Share

In game buttons using the new tools in Unity 4.6.

avatar image Thewhiteaura · Mar 21, 2015 at 04:19 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adrianrodriguez · Jan 05, 2015 at 08:09 PM 0
Share

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.

avatar image Mmmpies · Jan 05, 2015 at 08:30 PM 0
Share

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.

avatar image ghost00AC · Jan 15, 2016 at 12:56 PM 0
Share

Woah, thanks to this, i manage to reduce the lines of codes in our game. Definitely a linebreaker. LOL :) :P

avatar image
0

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 :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adrianrodriguez · Jan 05, 2015 at 08:14 PM 0
Share

Thanks, I will also check this out.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges