Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ExtremePowers · Jan 09, 2015 at 08:12 AM · variablefunctionparameter

Function as parameter?

Is this possible? Would it look something like this?

 public void Hey() {}
 
 public static void Hat(k) { /*What type is the function?*/
     Hey = k;
 }
 
 public static void Run() {
     Hey();
 }
 
 //How would I call this?
Comment
Add comment · Show 3
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 Andres-Fernandez · Jan 09, 2015 at 08:24 AM 1
Share

$$anonymous$$aybe you can google for "lambda expressions" and "delegates". That's a starting point.

avatar image Fornoreason1000 · Jan 09, 2015 at 09:03 AM 0
Share

I think i did something with generics too, this might help you

http://stackoverflow.com/questions/16184014/how-to-use-classes-themselves-as-method-parameters

other than that most people would use lambda expression and Delegates like Andres said

avatar image ExtremePowers · Jan 09, 2015 at 09:06 AM 0
Share

I have tried something with delegates, but I could assign Hey to be k with it?

1 Reply

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

Answer by Glurth · Jan 09, 2015 at 09:22 AM

I like using Action, nice and simple. (you can use Action < T > instead, for functions with a parameter)

 static class ActionTest
 {
     public static Action Hey;
     public static void Hello(){Debug.Log ("Hello");}
     public static void Hola(){Debug.Log ("Hola");}
     
     public static void Hat(Action k) { 
         Hey = k;
     }
     
     public static void Run() {
         Hat (Hello);
         Hey();
         Hat (Hola);
         Hey();
     }
 }
Comment
Add comment · Show 3 · 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 ExtremePowers · Jan 09, 2015 at 02:05 PM 0
Share

It says that Action isn't a valid type (error CS0246)

EDIT: Nvm I just had "using System.Collections" ins$$anonymous$$d of "using System". But now I have 2 new errors:

 public static Action OnYes() {} //error CS0161: `Game.OnYes()': not all code paths return a value.

 public static void Popup( string text, Action onYes) {
     instance.ShowPopup = true;
     instance.PopupText = text;
     OnYes = onYes; //error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer
 }
avatar image fafase · Jan 09, 2015 at 02:16 PM 0
Share
  public static Action OnYes() {}

this declares a method returning an Action type object, I think you want to create a delegate of type Action:

  public static Action OnYes = ()=>{};
avatar image Glurth · Jan 09, 2015 at 06:14 PM 0
Share

Perhaps you meant somthing like this?

 public static void OnYesFunction() {} 
 public static Action OnYesAction;
 
  public static void Popup( string text, Action onYes) {
      instance.ShowPopup = true;
      instance.PopupText = text;
      OnYesAction= onYes; 
  }

now we call Popup like this:

 Popup("blah",OnYesFunction);

Note, the action is not actually executed in the code above. To actually run the function in Popup, rather than store it for later, just replace

   OnYesAction= onYes; 

with

   onYes();

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

28 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

Related Questions

Passing a Script Name to a Function 2 Answers

iTween - calling functions on oncomplete doesn't work if the function is declared as a variable 1 Answer

How to correctly send a variable to another function? 1 Answer

Calling non applied scripts 3 Answers

Cleaning up my item script... Passing script name to function? 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