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 JPLeo · Oct 26, 2018 at 11:36 PM · buttoncanvas

Passing Object Reference to Button

In my script i want to pass the gameObject which contains it to the Object reference in a Button's OnClick event. alt text

is it possible to do that? I looked through the Button page and couldnt find anything mentioning the Object reference and it seems the AddListener method can only pass other methods.

questionpic.png (7.8 kB)
Comment
Add comment · Show 5
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 · Oct 26, 2018 at 11:51 PM 0
Share

I don't really get what you are trying to do....

You can't "add a reference to an object" to the onClick event. You add a function (callback). You can add a function defined on any object you want as far as you have a reference to the latter.

 public Weapon Weapon; //supposing you have a script called Weapon
 public Button button;
 
 void Start()
 {
     button.onClick.AddListener( () => Weapon.Fire() ) ;
 }
avatar image JPLeo Hellium · Oct 27, 2018 at 04:20 AM 0
Share

I have this barebones right click menu, when i click one of the buttons on it i want to do something with the UI object i just right clicked, there might be more than one such object so i figured the best way to make sure the code has the right object is somehow passing that object to the Button component, as if i dragged the object in the editor.

avatar image JPLeo Hellium · Oct 27, 2018 at 04:25 AM 0
Share

A few things i forgot to mention, everything is in the same script, the right click menu stuff and the function i want the buttons to call as well, so on the editor i dragged the prefab of the UI element that receives the right click, what i need is that when i right click, that specific object that i right clicked is sent to the buttons somehow and replaces the prefab.

avatar image hexagonius · Oct 27, 2018 at 02:05 PM 0
Share

same here, I don't get it.
As far as I understand, you have a menu with buttons (maybe a prefab). There can be more than one instance of that prefab. Each menus buttons should do something with their menu. If that's the case, from the buttons, call methods that reside in their menu on some script. This way every method call you do with a button works on THAT menu...
Or am I missing somethin here?

avatar image JPLeo hexagonius · Oct 27, 2018 at 09:28 PM 0
Share

Its pretty much that, but i want to avoid creating custom scripts for the buttons if possible. And i should've posted this image right away sorry about that, i think i forgot about it. alt text

I want the buttons in that little right click menu to call the equip and destroy functions on that "Axe" object, which contains all the item information itself. Naturally there might be more than one item object, so i need the buttons to work on that exact object.

questionpic2.png (45.5 kB)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jordash · Nov 01, 2018 at 12:05 PM

If I'm understanding correctly you can solve this by having the item which instantiates the menu pass a reference to itself to the new menu object.


This can be a reference to the GameObject or the script type, but I would suggest the script because you're likely to want access to the functions of the script, and the script can call its game object with more certainty that a game object getting the script.


With a bit more detail, you will need a public variable in the menu script which is the type of your item script. When the item instantiates the right click menu, it can follow that up by setting this public variable to itself. Something like:


 GameObject rightClickMenuInstance = Instantiate(rightClickMenuPrefab);
 rightClickMenuInstance.itemRef = this;

Then in the button onClick of the menu you can do whatever you wanted to do to the original item.


Have a look at inherited classes or interfaces in the C# documentation if you want to have multiple scripts which all can all conform to a common type.

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 Vega4Life · Nov 01, 2018 at 04:49 PM

Just create a function in your script that requires a GameObject parameter (for the button you want to pass to it). The script needs to be on the object you are referencing for the OnClick() event (which is a weapon object in your case). Then, set the OnClick() reference to the object with the script (a weapon object). Then click on the "No Function" drop down, find your script, then find your function callback. Next, drag the button object into the GameObject reference that the callback requires.


The function in the script on the weapon would look like this:

     public void ScriptThatIsListening(GameObject go)
     {
         // Do something with the ref to button
     }


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

120 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

Related Questions

Problem with Button in ScrollView 0 Answers

I Have a Button in Canvas, i Click anywhere except the button and it gets clicked 0 Answers

Hidden Canvas, doesn't disable interaction with buttons. 1 Answer

Detect if player wants to pause or to jump 3 Answers

If button highlighted 2 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