- Home /
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;
}
}
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");
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.
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
Answer by justinyeh · Oct 21, 2016 at 04:37 AM
You can call all public void from other script.
rootScript.<Any Void> ();
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.
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.
Your answer
Follow this Question
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
Distribute terrain in zones 3 Answers