- Home /
Invoke other script's function
Hi, i want to use this fucntion in script1
if(!IsInvoking("function of script 2"))
Invoke("function of script 2",0.5f);
with a function that is in the script2.
I wrote if(!IsInvoking("script.updateScore")) Invoke("script.updateScore",0.5f);
but it doens't work, what i should do ?
Thank you :)
P.S. I don't want to move the function in script1 and use script.variables ;)
Answer by rutter · Sep 12, 2014 at 05:20 PM
If you reference another script object, you can have it call Invoke instead of calling it yourself:
//calls my Foo()
Invoke("Foo", 2f);
//calls some other object's Foo()
someOtherObject.Invoke("Foo", 2f);
As an alternative, you could add a function to the other script that calls Invoke internally:
//in script 1
someOtherObject.DoTheThing();
//in script 2
public void DoTheThing() {
Invoke("Foo", 2f);
}
Coroutines work out the same way. It's ultimately whichever object that calls Invoke (or StartCoroutine) that ends up owning the context. By default, you'll call that in your own context, but it's possible to grab someone else's and have them call it.
What if you want to use Invoke and the void you want to invoke has value as well? I know it's wrong but it may help you understand what I'm trying to do!
GetComponent<OtherScript> ().Invoke ("DoSomething" ("Name"), 2f);