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 IronPig_ · Jun 19, 2017 at 07:58 AM · keycodemethodsexecute

Can I have a method as a parameter ?

Hi,

I wanted to create a method whics executes a method on a keypress. Something like this:

 public void OnKeyPress(KeyCode kc, Method stuffToExecute){
 if(Input.GetKeyDown(kc){
 
 stuffToExecute();
 }

What should replace Method stuffToExecute ?

Or is it possible somehow ?

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 Woltus · Jun 19, 2017 at 11:18 AM 1
Share

You can use delegates.

avatar image Owen-Reynolds · Jun 19, 2017 at 01:20 PM 0
Share

Delegates are clunky and a little obsolete. As Elarm writes, System.Func/Action is a nicer way, but also clunky (they both do the same thing, but you have to use one of the other depending on the function.)

The whole idea is known as Function Pointers. It's a common language feature, and works exactly as your example. You can even do more, like store a function in a variable. $$anonymous$$y favorite explanation is in the Function Pointer chapter at taxesforcatses (but it's like 15 pages.)

avatar image Glurth Owen-Reynolds · Jun 19, 2017 at 05:16 PM 0
Share

Cool site, bookmarked, thx! (Couldn't find the author's info.)

2 Replies

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

Answer by GamerJoHo · Jun 19, 2017 at 08:45 AM

Hi,

If you pass in a string, you can Invoke the method.

Such as:

 public void OnKeyPress(KeyCode kc, string stuffToExecute){
  if(Input.GetKeyDown(kc){
  
  Invoke("stuffToExecute", 0.01f)
  }

I write 0.01f to invoke it then, as i'm not sure 0f will be accepted. Your turn to play with that one!

Comment
Add comment · Show 1 · 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 IronPig_ · Jun 19, 2017 at 05:59 PM 0
Share

Thanks, you helped me a lot. :-)

avatar image
4

Answer by Elarm00 · Jun 19, 2017 at 12:34 PM

Yup!

You can use System.Action, ex.

 //---------------------------------------------------------
 public void OnKeyPressed(KeyCode keyCode, System.Action func)
 {
     if (Input.GetKeyDown(keyCode))
         func();
 }

You can even pass parameters, ex.

 //---------------------------------------------------------
 public void OnKeyPressed(KeyCode keyCode, System.Action<KeyCode> func)
 {
     if (Input.GetKeyDown(keyCode))
         func(keyCode);
 }

Also check out System.Func and delegates.

Comment
Add comment · Show 1 · 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 IronPig_ · Jun 19, 2017 at 05:58 PM 0
Share

Thank you. :-)

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

70 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

Related Questions

Mysterious Extra KeyUp Event (5.3.3f1) 2 Answers

TPSCamera rotate player when I don't want it 0 Answers

C# What is more efficient, If Statement inside Method or Method Inside of If Statement 0 Answers

Euler angles clamping issue 0 Answers

How to make custom input key presses 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