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 DVxAvG · Jul 15, 2018 at 02:07 AM · listmethodactionparameter

How can I make a System.Action List that takes Methods with parameters?

So I have:

public List <System.Action> unitQueue = new List<System.Action>();

It functions how it's supposed to, It takes methods and has the script perform the methods given in the list. There's one thing I want to do, but I'm not sure how to do it, and it's to have these methods take parameters.

For example, I've tried: unitQueue.Add(ExampleTask(SelectedGameObject)); but errors come up.

Those errors are: Argument "#1" cannot convert "void" expression to type "System.Action" and The best overloaded method match for "System.Collections.Generic.List<System.Action>.Add(System.Action)" has some invalid arguments

I'm not sure what do to here. Any help?

Comment
Add comment
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

1 Reply

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

Answer by Stratosome · Jul 15, 2018 at 03:17 AM

Hola,

So, System.Action is a delegate. In my words, I'd say it is like declaring a variable data type for some type of function you want. Personally, I would avoid using System.Action because it seems a bit more vague and would instead create my own delegates. Alright, so here's how you could use delegates:

     // A declared delegate with two string params
     public delegate void MyDelegate(string str1, string str2);
 
     public List<MyDelegate> someList = new List<MyDelegate>();
 
     void Start() {
 
         // --- ADDING FUNCTIONS TO THE LIST
 
         someList.Add(Function1); // (Add existing function)
         
         someList.Add((string s1, string s2) => { // (Add inline function)
             Debug.Log("Function2: " + s1 + " " + s2);
         });
 
 
         // --- CALLING THE FUNCTIONS IN THE LIST
 
         for (int i = 0; i < someList.Count; i++) {
             MyDelegate d = someList[i];
             // You pass in the parameters HERE (only when you call the function itself)
             d("Hello", "World"); 
 
             // Or you could do this: 
             // someList[i]("Hello", "World");
         }
 
     }
 
     private void Function1(string s1, string s2) {
         Debug.Log("Function1: " + s1 + " " + s2); 
     }


This code is going to add two different functions to your delegate list.


Unfortunately, you can't pass in the parameters EXACTLY like you were wanting to, but there are ways to kind of do it. Parameters can only be passed to it when the function is actually called. What you could do, is store those parameters that you want into a little struct or something and add it to the list instead. It could look like:

     // A declared delegate with two string params
     public delegate void MyDelegate(string str1, string str2);
 
     public struct FunctionForLater {
         public string str1;
         public string str2;
         public MyDelegate del;

         // Constructor
         public FunctionForLater(MyDelegate func, string s1, string s2) {
             str1 = s1;
             str2 = s2;
             del = func;
         }
     }
 
     public List<FunctionForLater> someList = new List<FunctionForLater>();
 
     void Start() {
 
         // --- Add a function with params for later
         someList.Add(new FunctionForLater(Function1, "Hello", "World"));
 
 
         // --- Calling the stored function
         FunctionForLater func = someList[0];
         func.del(func.str1, func.str2);
 
     }

Hopefully that kind of makes sense. Let me know if it doesn't!

Comment
Add comment · Show 6 · 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 DVxAvG · Jul 15, 2018 at 03:36 AM 0
Share

Thank you for the reply! I'll get back to you once I put everything in and test it :)

avatar image DVxAvG · Jul 15, 2018 at 07:18 PM 0
Share

It's starting to work well, still adjusting everything. I have another question, how can I check if someList contains a function at someList[0], I think I'm doing something wrong.

Thank you again for replying earlier! It really helped.

avatar image Stratosome DVxAvG · Jul 19, 2018 at 08:58 AM 0
Share

@DVxAvG

AC$$anonymous$$, sorry, I didn't look back at this question to check for another response. Um, how to check to see if, for example, someList[0] has a function? Why are you trying to check for that? I mean, you could see if someList[0] == null or if someList[0].del == null. I kinda doubt that's what you mean. If you clarify that question a bit, I'd be more than happy to respond unless you've already got something figured out.

avatar image DVxAvG Stratosome · Jul 20, 2018 at 10:45 PM 0
Share

Oh! Well the response you just made actually helped me! I was being an idiot and only used "if(someList[0] == ExampleTask)", I didn't know I had to include the .del after someList[0]. I wanted to check someList[0] so when it contains the function I'm looking for, I can call some stuff. Sorry I didn't clarify well, but you helped again. Thanks lol.

Show more comments
avatar image Samec6523 · Feb 20, 2021 at 09:09 PM 0
Share

U have saved me a lot of work!!!

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

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

Weak System.Action iOS 0 Answers

need help stopping these lag spikes when my list iterates 1 Answer

How to List coordinates in a queue?,How to make a queue list of coordinates? 0 Answers

Assign action to UI button 0 Answers

How to add one onClick Event to multiple UI buttons 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