• 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
Question by Xx_Alanox_xX · Nov 01, 2017 at 10:25 PM · buttonscript.

Unity Button Script

Hello, i wanted to know if there is any ways to fill a function and the parameter of it without using AddListener :

alt text

i used an AddListener for my script currently but i believe that if i fill in these by sript, it would be more efficient, thank you for your responses ! :)

unity-help.png (6.4 kB)
Comment

People who like this

0 Show 11
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 Xx_Alanox_xX · Nov 01, 2017 at 10:27 PM 0
Share

when i say it, i mean the OnClick event ;)

avatar image Hellium · Nov 02, 2017 at 09:23 AM 1
Share

I don't understance what you are trying to achieve.

The OnClickEvent of a button can be assigned using the inspector (drag & drop the gameobject and choose the function), or by script using the button.onClick.AddListener( listener ). There is no other way.

avatar image Xx_Alanox_xX · Nov 03, 2017 at 01:02 AM 0
Share

I basically instatiate a gameobject and want to give it an onclick event, but when the "button" is clicked, i'd like it to pass in the gameobject as a parameter so i can use it in my script

avatar image Xx_Alanox_xX · Nov 03, 2017 at 01:06 AM 0
Share

i can go

 AddListener( delegate { method(parameter);  )

but i would have just liked to know if there are any ways to add the button via script, then fill in the different slots on the "OnClick()" manually... anyways if you say there's no other way, thank you :)

avatar image Hellium Xx_Alanox_xX · Nov 03, 2017 at 06:36 AM 1
Share

Oh, I see now.

It's possible to do so in the inspector but only for public functions "returning" void with only one parameter which is either an int, float, bool or string I think.

Otherwise, you will have to do it by code indeed. But I may be wrong, maybe, some 3rd parties plugins focused on Serialization (such as Odin) can do this without code.

avatar image Xx_Alanox_xX Hellium · Nov 03, 2017 at 02:36 PM 0
Share

Yes, the only problem is that i don't know how to set the value for an onlick event from the script... i want to basically do the same thing as in the inspector ( add an object -> get the component -> give it a function with parameter ) so when i click on it, it would return a value. As i said, i used AddListener( delegate { method(parameter); ) but it doesn't seem to be working, i tried searching and in found this link text

Now, so let me explain :) i have different "Equipment" and in a function, i try to find the slot index of an "Equipment" : public void AddEquipmentSlotUI(Equipment equipmentAdd, int slotIndex) { if (slotIndex == 0) { newEquipmentSlotBox = Instantiate(equipmentSlotBox, transform.position, transform.rotation);

 // as i read in the link above, i made another GameObject and set its value to the newEquipmentSlotBox so i could try and get its correct value everytime
             GameObject newGO = newEquipmentSlotBox;
 // this is where i want to add a button --> with "addListener"
             newEquipmentSlotBox.GetComponent<Button>().onClick.AddListener(delegate { RightEquipmentAdd(newGO); });
         }
      }

What happens everytime i "click" on one of these Instantiated gameobjects is that it doesn't return the correct "Equipment" of the correct "slotIndex", this means that when i add the "AddListener", the "newGO" is always = to the LAST equpment i added to my list

Show more comments
Show more comments

0 Replies

· Add your reply
  • Sort: 

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

132 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

Related Questions

how to change a buttons function with a script? 2 Answers

How to enable and disable back game object for ui button after a few seconds? 1 Answer

Basic scripting 1 Answer

How to click button multiple times 0 Answers

When you click the same image twice it see it says a match. Any thoughts on how to fix that? 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