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 /
  • Help Room /
avatar image
0
Question by Deathex565 · Feb 25, 2017 at 11:59 AM · uibuttononclick

Get value in button OnClick()

Hello,

I'm developing an RTS which has buildings that the player has to first unlock before they can be placed. I'm alright with having the buttons that construct those buildings visible but I want them to be non-interactive. The way I'm using the buttons is that in each I use the OnClick() to call a function in a script and pass the gameobject to be constructed, which I set in the inspector.

Basically I want to have a button manager script where I check for each button whether the gameobject assigned has a specific parameter, is it possible for me to do that?

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 hexagonius · Feb 25, 2017 at 01:19 PM 0
Share

you mean: "iterate all buttons, get their assigned gameobjects and check for parameters"? if so, what exactly do you mean with parameter? methods have parameters.

avatar image Deathex565 hexagonius · Feb 25, 2017 at 02:54 PM 0
Share

I mean, iterate through a public array of buttons which I will assign from the inspector, and get the gameobject that's assigned to be sent to the function that's called when OnClick() happens.

Basically, my function is defined as setItem(gameobject g), and is part of a BuildingPlacement script which is a component of a Player gameobject. I've assigned the Player to the buttons in question, selected the function mentioned above from the list of functions in the script mentioned above, and assigned a gameobject as a parameter for that function. What I would like to do is get that gameobject for every button I specify.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by hexagonius · Feb 25, 2017 at 03:42 PM

ok. the should be:

 foreach (var b in buttons){
   GameObject target = b.ButtonClickedEvent.GetPersistentListener(0) as GameObject;
   Debug.Log(target.name);
 }

this works for one receiver on each button. if there's more you'd need to loop the function.

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 Deathex565 · Feb 25, 2017 at 04:35 PM 0
Share

Thank you, however I'm getting "ButtonClickedEvent cannot reference a type through an expresion", and "Button.ButtonClickedEvent does not contain a definition for GetPersistentListener". I have another idea as to how to do what I want even though it's less dynamic, but still curious to see if it's possible to get the parameter that a button will send to a function if clicked.

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

134 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

unity 2018.3.12f1 Button OnClick call function on another instance? of script 0 Answers

UI Buttons without blocking raycast 0 Answers

How do I make a UI Buttion play an Animation when pressed? 0 Answers

AddListener not working after build 0 Answers

Trying to increment a variable in a Coroutine on pressing a UI button 0 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