• 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
2
Question by Lovrenc · Sep 09, 2014 at 10:06 AM · uionclickevent handlers

ugui, onclick, acceptable function signatures

Hello

What kinds of functions are acceptable for ugui onClick event? So far I noticed that return type must be void and function can have either no parameter or a single primitive parameter string, boolean,...

Do you guys know any definitive rules that would give me the big picture?

Can I use enum as parameter for instance?

Thank you.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by OrbitGames · Sep 09, 2014 at 05:37 PM

They say it in the youtube tutorial at 10:30

It must be a public void. it can have 0 or 1 parameter, which can be of the type float, int, string, bool or unity object.

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
-1

Answer by BrainWashG · Sep 09, 2014 at 05:59 PM

Pass it through an int and cast it as an enum when you get it back, you can also give an "Object" Parameter which give you most of the others parameters like GameObject transform and every single object inherited from it

PS : sorry for my english

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 Lovrenc · Sep 09, 2014 at 06:04 PM 0
Share

This is not cool, because it enables a lot of errors during programming. I would also have to remember all the enum types and their order.

avatar image BrainWashG · Sep 09, 2014 at 10:18 PM 1
Share

well lot of errrors? not really and you don't have to remember the enum types, its written on your code... tho there is no other solution to give an enum parameter, maybe i believe you can use a unity object

avatar image Lovrenc · Sep 09, 2014 at 11:07 PM 0
Share

I appreciate your effort, but this is nothing short of a bad solution. And after all, its not enum anyway, I might as well use different function which would hold target component as a parameter with common interface or something else...

Yes I don't need to memorize the enum, but I would have to go search for it in the code. This would also move error detection from compile time to run time. It is just bad to deprive yourself of all the candy that .NET and unity editor are offering you. Unless its a micro project, you are going to have a bad time.

avatar image jister Lovrenc · Apr 06, 2016 at 02:37 PM 0
Share

@BrainWashG's solution is perfectly valid and the way to go if an OnClick event needs to set an enum! ask for int or string as a parameter and cast it to the enum.

"Nothing short of a bad solution..." is really misplaced here because you never even gave a problem to start with, you just asked if enum's could be used as overload for an OnClicklistener, so: no not directly but yes if you follow BrainWashG's method.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

26 People are following this question.

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

Related Questions

UGUI OnClick Event! 1 Answer

Any way to show a picture on the screen when a model is clicked? 2 Answers

Passing through a GameObject/Function to a button's OnClick 1 Answer

Press button on canvas to transform prefab clone? 0 Answers

GUI button to Unity5 Button OnClic function 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges