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 albertoni · Jan 07, 2016 at 10:10 PM · c#gameobjectonclickparameters

Passing two GameObjects to a C# onClick script

Hi guys. I'm doing a simple point and click adventure game.

As an example, at some points you will have a choice between a few different options.

What I'd like to do is, when you press a button for one of the choices, to make the button disappear, a textbox appear and some text to be printed. I can do everything I want, but the way is very inelegant. I've stuck 3 onClick scripts on the buttons. One saves the button GameObject in a class variable, the second saves the textbox GameObject and the third fires the function that prints the text with the text ID.

Is there any way I can put more than an argument in an onClick handler, so I don't have to save everything, possibly suffering with race conditions later on?

Here's the relative code so you can see what I'm doing: http://pastebin.com/a23Du31g

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 AlexanderAsmussen80 · Jan 08, 2016 at 07:44 AM 0
Share

Hi,

I don't get it right... In your code there is no OnClick function. And if you use three scripts with OnClick you can put this code together in one function. It would be helpfull, for a better understanding, if you could post the real code and maybe a screenshot how the scripts are attached to the button.

avatar image albertoni AlexanderAsmussen80 · Jan 10, 2016 at 05:50 PM 0
Share

Here's a screenshot of the relevant part. Sorry for the delay. http://i.imgur.com/AQa8vLj.jpg

Restating my problem, see on the right how there are 3 scripts with 1 argument each? I couldn't find a way to put multiple arguments to it.

I could find the button and textbox dynamically, but I feel my way is more future proof.

1 Reply

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

Answer by hulahoolgames · Jan 10, 2016 at 06:34 PM

What I usually do is create a UI Panel for each main UI screen and put the elements of that UI inside this panel. So you can create a Panel if you wish and put all the buttons and text in this panel. Now on the panel you can add a script, or you can add it anywhere you like (if you dont want to create a panel):

 public GameObject button;
 public GameObject text;
 
 public void OnButtonClick() {
      button.SetActive(false);
      text.SetActive(true);
      text.GetComponent<Text>().text = "Hello World!";
 }

Now add this function as the onclick to the button and attach the button and text gameobjects to the variables. Hope this helps!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Moving GameObjects on elliptical way after clicking on them 1 Answer

Multiple Cars not working 1 Answer

Button On Click with object from different scene 0 Answers

Distribute terrain in zones 3 Answers

Refering to gameobject script is attached to 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