Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by franbat · Sep 21, 2015 at 09:32 AM · 2dprogrammingbuttons

Programming 2D buttons

Hello, I have a game which has some 2D buttons in the main. Those buttons were made in another program and I added them in a prefabs folder. The problem I have is those are several buttons, so to program them the only way I know is making a script with the function OnMouseDown() for each button. Is a better solution like making a general script in the camera or in an empty GameObject for everything?? Thank you very much

Comment
Add comment · Show 1
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 Hellium · Sep 21, 2015 at 01:19 PM 0
Share

Each button may have a different behaviour if you click on one or another, right ?

Then, you will have to implement On$$anonymous$$ouseDown for every button.

Please, be more specific if you want help.

3 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by amanpu · Sep 21, 2015 at 02:00 PM

There is no need to make separate script for each button. You can make only one script with only OnMouseDown() method. And add this script to all the buttons. Then you can differentiate between buttons using switch statement as following:

 void OnMouseDown()
 {
     switch(gameObject.name)
     {
         case "Button1":
             // Do something related to Button1 here
             break;
         case "Button2":
             // Do something related to Button1 here
             break;
     }
 }

 


But make sure you are not doing anything else in this script. Because this script might be attached to many GameObjects and all the methods like Start(), Update() are invoked individually for each GameObject. This will eventually lead to problems e.g.:

 void Start()
 {
     GameObject player = GameObject.Find ("Player");
     player.SetActive(false);
 }


Above method will cause a problem, if this script is attached to more than one GameObject then first time it will find the "Player" and disable it. When this method is invoked again second statement will throw Runtime Exception because it will not be able to find disabled GameObject.

Comment
Add comment · Show 4 · 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 Hellium · Sep 21, 2015 at 02:45 PM 0
Share

Even if this solution works, it is a very dirty way to do things.

Supposing you have hundreds of buttons Change a button name ? Add a new button ? Delete one ? You have to find your button in the mess you have created inside the On$$anonymous$$ouseDown function.

As you said, if a button has a Start button that must not be executed by the others, you have to make conditionnal statement. The same as above, change / add / delete a button, and you have to change your whole script

This code is absolutely not maintainable ...

I know, some arguments here can apply if you have a thousand scripts, one for each button. But there are some ways to use abstract classes so as to make the job less painful.

avatar image franbat · Sep 21, 2015 at 02:47 PM 0
Share

Hello! Yes, each button have their own function. I have used in another part (maybe I'll consider what you wrote later) something that the name is Physics.Raycast into the script and then I added it to the camera. But in that case were only three objects that I had to detect; not all the buttons I mentioned. It detects when an object has a collider and then it applies what you want:

 void Update(){
         if (Input.Get$$anonymous$$ouseButtonDown(0)){
             Ray ray = camera.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit)){
                 Destroy(hit.transform.gameObject);
             }
         }
     }






Thank you very much!

avatar image amanpu · Sep 21, 2015 at 02:56 PM 0
Share

@Hellium ,So what is best solution then?

$$anonymous$$y argument is there should be one script just for gettings clicks. Now for example you need to play sound on button click, Is it justify to write same line that is audioSource.Play in all scripts?

But yes working of all buttons should not be defined here, It should be defined in some other script say Game$$anonymous$$anager using delegate callbacks. But I think click should be in one script rather than in thousand scripts.

avatar image Hellium amanpu · Sep 21, 2015 at 04:49 PM 0
Share

And here is the utility of abstract classes ! ;) Just call the audio play function in the Start method of the abstract class, and make the scripts derive from this class and you have to write only one line.

click should be in one script rather than in thousand scripts

What do you mean ? If the logic behind every button is different, they should have their own script, this is the way I see things.

avatar image
0

Answer by DiegoSLTS · Sep 21, 2015 at 02:58 PM

In Unity you add components to add behaviour to game objects. If you have buttons, add components to the buttons. If more than one button does "the same" (same code with probably diffrerent values) add the same component to them. If another button does something different, add another component.

You can add a script to the camera to handle everything, but it goes against the Component Based design. You're not forced to use that approach though, but it makes things easier if you do, since the engine was created around that concept.

Look at how uGUI works, each button has it's "OnClick" event, so each button "knows" what it should do when clicked. Note that uGUI uses an EventSystem game object that actually detects what is clicked, but that object does only that, it just tells the object "hey, you were clicked, do what you have to do".

Comment
Add comment · 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
0

Answer by franbat · Sep 21, 2015 at 07:49 PM

@Hellium, what @amanpu suggested is something that is a good idea in the case that you write "This button makes this". I used the Physics.Raycast beacuse in that case I had to make some things in that code. But the thing I'm agree with @Hellium is that I don't like the idea of adding one script for all the buttons.... It depends on how do you like to program

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Cycled map (world) 0 Answers

2D Animation does not start 1 Answer

Why doesn`t my GUI Button show up? 1 Answer

How do I make a button interactible when it's overlaped with an other button ?,How to make a button hit area fit the shape of its image 1 Answer


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