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 /
avatar image
0
Question by JesseFox · Oct 21, 2016 at 03:53 AM · c#functionstring

Getting function from other script?

Does anyone know how i would be able to access functions in other scripts by a string? so the idea behind this is in the inspector on the second script i just enter in the string section what function i want to call from its root script instead of have to manually coding rootScript.valueOne, rootScript.valueTwo or rootScript.valueThree in the second script. I've tried rootScript, rootScript.(calledFunction), rootScript."calledFunction". so far nothing is working :(

script one

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class scriptOne : MonoBehaviour {
 
     public int valueOne;
     public int valueTwo;
     public int valueThree;
 
     void Start () {
         
     }
 
     void Update () {
 
     }
 }

script two

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class scriptTwo : MonoBehaviour {
 
     public  scriptOne rootScript;
     public int testInt;
     public string calledFunction;
 
     void Start () {
         
     }
 
     void Update () {
             testInt = rootScript.calledFunction;
     }
 }





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

3 Replies

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

Answer by b1gry4n · Oct 21, 2016 at 04:31 AM

My suggestions isnt exactly what you want, but if youre dead set on using a string to determine specific actions, it could be done like this...

 public string stringVal;
 
 void Start(){
     Run(stringVal);
 }
 
 public void Run(string s){
     if(s == "whatever1"){
         //do whatever 1
     }else if(s == "whatever2"){
         //do whatever 2
     }else if(s == "whatever3"){
         //do whatever 3
     }
 }

and called from another script would just be

 theScript.Run("whatever1");
Comment
Add comment · Show 4 · 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 JesseFox · Oct 21, 2016 at 04:53 AM 0
Share

your script will work but I'm just looking to set a int in one script from another script without having to manually code script '.int' or script '.int2 ' ins$$anonymous$$d have a string that calls that ' .whatever'. so for example if the string = 'int1' it will then call script.(string) which if i manually had to code it, i would had to code script.int1.

avatar image b1gry4n JesseFox · Oct 21, 2016 at 05:26 AM 0
Share

Again, not exactly the thing you want 100%...but

script one:

     public int val1;
     public int val2;
     public int val3;
 
     public int GetVal(string s)
     {
         int val = -1;
         if (s == "int1")
         {
             val = val1;
         }
         else if (s == "int2")
         {
             val = val2;
         }
         else if (s == "int3")
         {
             val = val3;
         }
         return val;
     }

script two:

 public ScriptOne rootScript;
 public int testInt;
 public string calledFunction;

 void Update()
 {
     testInt = rootscript.GetVal(calledFunction);
 }

You might want to look into unity events/actions. heres another question i answered...

http://answers.unity3d.com/questions/1256575/assign-unityevent-by-code-c.html#answer-1256702

Ive never tried to do anything like this, after searching online it looks like it requires reflection.

http://stackoverflow.com/questions/540066/calling-a-function-from-a-string-in-c-sharp

avatar image JesseFox b1gry4n · Oct 21, 2016 at 05:49 AM 0
Share

ill try this method out :)

Show more comments
avatar image
0

Answer by justinyeh · Oct 21, 2016 at 04:37 AM

You can call all public void from other script.

  rootScript.<Any Void> ();
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 JesseFox · Oct 21, 2016 at 04:58 AM 0
Share

im not looking to call a void just get the rootScript.'int' via a string. its just finding out how i can get it to read a string and use that to call functions with the same name from other scripts.

avatar image
0

Answer by JedBeryll · Oct 21, 2016 at 06:01 AM

Easiest way if you want to call a function is to use messages. This method calls the method with the given name and it takes an optional parameter. Messages will be sent to the whole gameobject and the function will be called in all the scripts on the object if they exist. Your other option is reflection, this can do much more than the SendMessage, but it's more complicated too. If you want to know more about it, ask in comment.

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

227 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 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 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 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

Find variable in another script using a string 1 Answer

Cast the result of an enum as a string 3 Answers

Multiple Cars not working 1 Answer

When issuing to print a private variable from the script, the script prints all the variables attached to objects using the same script instead of the specific object. How do I fix this? 3 Answers

Distribute terrain in zones 3 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