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 poofdragon · Aug 23, 2012 at 09:16 AM · c#variablesreflectionmethod

Use a String Variable to Dynamically Invoke a Method

I'm trying to use a string variable to assign which method to call which will then return a Integer value. I can assign the variable using the following simplified example:

 int IncomingValue = some.other.method();
 int TargetValue = 0;

 Update(){
     TargetValue = IncomingValue;
 }


This returns an updating value based on the result of some.other.method(). This works from a functional standpoint, but what I want to do is assign a string in place of some.other.method() so that I can dynamically assign which method to call in Update(). As in the following example:

 string IncomingValue = "some.other.method()";
 int TargetValue = 0;
 
 Update(){
 TargetValue = IncomingValue;
 }

I have found a number of articles referring to the use of Reflection or Delegates, but I can't get any of the examples I've found to work. Basically what I need is some way of Invoking(IncomingValue) and returning the result to TargetValue.

Any suggestions?

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
0

Answer by senad · Aug 23, 2012 at 10:01 AM

There is no need for reflection. You want to use delegates to define custom methods.

Your variable will be the type of your delegate instead of string.

Comment
Add comment · Show 8 · 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 poofdragon · Aug 23, 2012 at 10:12 AM 0
Share

Could you give me an example of this? I've found several different methods while researching this issue, and I still haven't been able to implement a working solution.

avatar image senad · Aug 23, 2012 at 10:28 AM 0
Share

With delegates your example would look like this:


delegate int input$$anonymous$$ethod() = some.other.method;
int targetValue = 0;

void Update() { targetValue = input$$anonymous$$ethod(); }

Have a look at this tutorial: http://msdn.microsoft.com/en-us/library/aa288459%28v=vs.71%29.aspx

avatar image poofdragon · Aug 23, 2012 at 11:02 AM 0
Share

I read through the tutorial you linked and I am completely lost. Example 2 seems like the closest to what I'm trying to accomplish, but I get the error "input$$anonymous$$ethod is a 'Type' but is used like a 'variable'".

avatar image poofdragon · Aug 23, 2012 at 11:11 AM 0
Share

Also, if I use your example:

delegate int input$$anonymous$$ethod() = some.other.method;

I get an error expecting a ; before the = sign:

delegate int input$$anonymous$$ethod();

avatar image senad · Aug 23, 2012 at 11:47 AM 0
Share

Oh yes, it is true. This is the type declaration:
delegate int Input$$anonymous$$ethodDelegate();

Then you can use it like this:


delegate int Input$$anonymous$$ethodDelegate();
Input$$anonymous$$ethodDelegate input$$anonymous$$ethod = some.other.method;
int targetValue = 0;

void Update()
{
targetValue = input$$anonymous$$ethod();
}
Show more comments

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Variables are assigned but it returns null 2 Answers

Calling a variable based on a variables name? 1 Answer

Are c# methods arguments stored as local variables? 0 Answers

Distribute terrain in zones 3 Answers

Problem with var in C# (Vector3, transform and similiar) 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