Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 HNKMaster · Oct 16, 2015 at 05:11 PM · animatorstringfunctionsanimation events

Using a string to call a function from another script without "SendMessage"

Hi everybody.

I'm trying to call functions using Animation Events and a sprite script (this manages the animations). in which I store the function names on a string array for faster work. The thing is, I could do this task using SendMessage which is faster and doesn't require long strings of code, but I know it tends to be memory expensive and sometimes, this get executed by a lot of instances.

What I want to do is, call functions from other scripts using strings without SendMessage. I want to call functions using something like this for example:

 public string funcName = "cScript.atkScript.StopAtk"
 
 //Later
 
 funcName(); //or something like that.


But I don't know how can I do that, and I don't want to write an event for every function my character needs. So, I need your help please.

Thanks.

Comment
Add comment · Show 1
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 meat5000 ♦ · Oct 16, 2015 at 06:05 PM 0
Share

Working in Strings is very slow. Perhaps rethink the logic and register delegates ins$$anonymous$$d.

Edit: Ok fafase already mentioned delegates. I'll upvote ;)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by fafase · Oct 16, 2015 at 05:59 PM

"I don't want to write an event for every function my character needs"

Wrong thinking there, short code does not mean better code. But back to your issue.

You could use reflection with GetMethod and the name of the method as parameter. You have the script type and you get a method on it:

  Type scriptType = typeof(MyScript);
  MethodInfo info = scriptType.GetMethod("Method");
  if(info != null)  info.Invoke(obj, new object[]{});

Looks quite similar to SendMessage though.

Finally, you can have a dictionary of . For each method you need to call, you register an entry in the dictionary with a name and a delegate. For instance, you have a method MyMethod:

  Dictionary<string, Action> dict = new Dictionary<string,Action>();

  GameObject obj = GetObject(); // Get an object reference
  ScriptType st = obj.GetComponent<ScriptType>();
  dict.Add("MyMethod", st.MyMethod);

and then:

  dict["MyMethod"]();

or

 Action action = dict["MyMethod"];
 action();
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 Herschelx · Jul 02, 2021 at 05:37 PM 0
Share

Sory to necro this post.

   Dictionary<string, Action> dict = new Dictionary<string,Action>();
   GameObject obj = GetObject(); // Get an object reference
   ScriptType st = obj.GetComponent<ScriptType>();
   dict.Add("MyMethod", st.MyMethod);

Is there a way to automate dict.Add("MyMethod", st.MyMethod); ? Would be really to tedious to add the string, method one by one if we have dozens of function.

avatar image
0

Answer by Herschelx · Jul 02, 2021 at 05:39 PM

You could also simply

  public string funcName = "cScript.atkScript.StopAtk"
  Invoke(funcName, time you want it to activate);

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

31 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

Related Questions

Animation events firing all at once in mirrored blend tree 1 Answer

Cannot select function for Animation Event 0 Answers

C# - parsed strings not changing in function 1 Answer

Animation Event Bug 1 Answer

2D Animation does not start 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