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 /
avatar image
0
Question by Bokaii · Jan 27, 2015 at 08:15 AM · uicanvasmenuunity 4.6

Canvas Button OnClik()

I have made a couple of menus in the same scene. I wan't, when I press a button, I open up another menu. I kinda know how to do that.

But I have a question.

In the "Menu Manager" script, where I define the functions the buttons are going to use, can you not do this:

 public void MenuSwap(**string name, Canvas thisCanvas**){
     //Do whatever
 }


can you not have 2 variables in the brackets??

Comment
Add comment · Show 2
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 meat5000 ♦ · Jan 26, 2015 at 10:54 PM 0
Share

You can and it works. They are called Function Arguments.

I don't understand why this gives you issues.

avatar image Bokaii · Jan 27, 2015 at 07:25 AM 0
Share

When I put 2 "function arguments" in, the function/ void disappears from the functions in the button list. I can't find it! :c

But of course, this is when I use the canvas button, OnClick(); Not when I use it normally.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kiwasi · Jan 27, 2015 at 08:16 AM

You can't have two variables. You can wrap both variables in a single UnityObject and pass that in.

Or you can add the listeners via script. You can add more variables that way.

Comment
Add comment · Show 7 · 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 Bokaii · Jan 27, 2015 at 08:27 AM 0
Share

Can you give an example in script? Because I didn't understand much of that?

avatar image AndresBarrera · Jan 27, 2015 at 09:39 AM 1
Share

What I think he means:

Option 1:

     public void $$anonymous$$enuSwap(Object data){
     //Do whatever
     }

And then you can have an object on your scene or a prefab with the data you want to pass. You reference this object as the paremeter on the onClick listener and then cast it inside your function to extract the data

Option 2:

 //Anywhere inside your code
     button.onClick.AddListener(() => $$anonymous$$enuSwap("$$anonymous$$y String", canvasReference));

I have only used AddListener for functions with no parameters, but maybe it could work like this. Don't forget to remove listeners later if you wont need them.

avatar image Bokaii · Jan 27, 2015 at 09:47 AM 0
Share

The problem with option 1 is, that I have more than 2 menus. And if I understood this correctly, I would have to have many objects or prefabs with my data... And I want a script with which I can use for every button, without having to change much. Like maybe just a name to search for. (the next menu.) But Then I would have to disable the first canvas, and enable the next. But I can't define the buttons parent canvas.

And I don't understand option 2. ;)

avatar image Kiwasi · Jan 27, 2015 at 10:49 AM 0
Share

Just add a script to the button with a few public variables holding the data. Pass that into your event trigger.

avatar image Bokaii · Jan 27, 2015 at 10:55 AM 0
Share

I tried this, and it seems like a lot of work, is there no other way to do it?

Show more comments

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

Unity 4.6 UI Lags on android 0 Answers

UI doesn't render after scene reload. 0 Answers

Scaling a menu bar 1 Answer

Basic question about creating a menu 1 Answer

UI menu not rendering properly 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