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 JustinC · Mar 17, 2015 at 12:48 AM · c#variablemethod

Storing a method in a variable

Hey All,

I am trying to store a method as a variable, but I don't know how. I have a weapon script that I am using to set all of my munition types, damages, ranges, etc. I used enums to list some of the basic weapon styles, such as missile, assault rifle, sniper rifle,etc. What I want to do is call one firing method for the rifles and a different firing method for the missile functions. I planned on storing the the method in a "var" according to a switch for the enum, but I get errors when I do that ( Error CS0815: Cannot assign void to an implicitly-typed local variable (CS0815)) how can I switch the method for firing depending on what my weaponType enum is?

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

2 Replies

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

Answer by raulrsd · Mar 17, 2015 at 01:16 AM

You need to use delegates.

First declare the Delegate and a variable to store the method:

 public delegate void FiringDelegate();
 FiringDelegate firingMethod;

Then you need to implement the method. As our delegate has been declared without parameters, the method can't have parameters either.

 void FiringSniper(){
       //DO WHATEVER
 }

Finally you could store the method in the variable and execute it this way:

 firingMethod = FiringSniper;
 firingMethod();

You can even pass the method as a parameter:

 //declare the method
 void ExecuteFiring(FiringDelegate miMethod){
      miMethod();
 }
 
 //call the method
 ExecuteFiring(FiringSniper);


Hope it helps

Comment
Add comment · Show 2 · 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 JustinC · Mar 17, 2015 at 03:28 AM 0
Share

This worked great!! thank you for your help. Just out of curiosity, I tried it with a IEnumerator and it didn't work. Is there a way to call a startCoroutine method this way?

avatar image raulrsd · Mar 17, 2015 at 09:02 AM 0
Share

You're welcome. I've never tried it with StartCoroutine but I've found this piece of code in unity forums:

 public class TestIng : $$anonymous$$onoBehaviour {
     public delegate IEnumerator WaitFunction(int f);
        
     void Start ()
     {
         WaitFunction func = Wait;
         StartCoroutine(func(2));
     }
    
     IEnumerator Wait(int f)
     {
         yield return new WaitForSeconds(1f);
         Debug.Log(f + " " + Time.time);
     }
 }

http://forum.unity3d.com/threads/delegates-and-co-routines-and-yield-waitforseconds.49221/

avatar image
0

Answer by Cherno · Mar 17, 2015 at 01:09 AM

You could store the name as a string and then use the SendMessage function to call that function along with up to one other parameter.

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Why the variable don't change your value? 1 Answer

Why do I get this error: "Assets/Scripts/Parkour Movement.cs(15,38): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected" 2 Answers

Accessing variable from a method in another script and gameObject 2 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