Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
1
Question by atul-s-morey · Jul 15, 2015 at 08:58 PM · c#onclickaction

Passing object as an argument to a System.Action

I am trying to create a feature that will allow developers to create a button and pass object as arguments.

Following set up works but I want to take this to new level where user can define object as an argument for the buttonAction

 public Button AddButton (string buttonLabel, System.Action buttonAction)
 {
 
         GameObject newButton;            
         newButton = Instantiate (ButtonTemplate); //ButtonTemplate prefab 
 
         //Change button settings
         newButton.name = buttonLabel;
         newButton.GetComponentInChildren<Text> ().text = buttonLabel;
 
         Button newButtonComponent = newButton.GetComponentInChildren<Button>();
 
         //Add a listener for the button action when it is clicked
         newButtonComponent.onClick.AddListener ( () =>  buttonAction ());
 
         return newButtonComponent;
 }

I am getting an error whenever I try to do this:

 public Button AddButton (string buttonLabel, System.Action<object> buttonAction, object args)
 
 {
      //..........same stuff
 
     //Add a listener for the button action when it is clicked
     newButtonComponent.onClick.AddListener ( (args) =>  buttonAction (args));
 
     return newButtonComponent;
 
 }

error CS0136: A local variable named args' cannot be declared in this scope because it would give a different meaning to args', which is already used in a `parent or current' scope to denote something else

Comment
Add comment · Show 1
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 troien · Jul 15, 2015 at 09:18 PM 0
Share

I'm not sure, but I think you want to remove the ', object args' at the end of your method parameters. Sinse you are setting the listener, you are not invoking the action and thus you don't need to send any arguments...

The error (CS0136) you get is because 'args' (declared in the lambda expresson) hides 'args' (declared as a method parameter).

I'm not sure if it would work after you remove this, but this error should at least be gone.

EDIT: This will probably still not work as the onClick of a button doesn't accepts listeners with a parameter I believe. So if you ins$$anonymous$$d want to send the args that you gave as parameter, I think changing '(args)' to '()' in the lambda expression ins$$anonymous$$d might work.

Like this

 newButtonComponent.onClick.AddListener( () =>  buttonAction(args));

1 Reply

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

Answer by atul-s-morey · Jul 16, 2015 at 01:19 PM

The answer is as simple as

 public Button AddButton (string buttonLabel, System.Action<object> buttonAction, object args)
  
  {
       //..........same stuff
  
      //Add a listener for the button action when it is clicked
      newButtonComponent.onClick.AddListener ( () =>  buttonAction (args));
  
      return newButtonComponent;
  
  }

To take this one step further...

  public Button AddButton (string buttonLabel, System.Action<object[]> buttonAction, params object args)
      
      {
           //..........same stuff
      
          //Add a listener for the button action when it is clicked
          newButtonComponent.onClick.AddListener ( () =>  buttonAction (args));
      
          return newButtonComponent;
      
      }

Now you have n number of arguments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

How to modify a button component with C# 0 Answers

An OS design issue: File types associated with their appropriate programs 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