- Home /
Duplicate Question
Call a Function from another Script
So, the thing is. I want to Call a Function in another Script without referencing the Object it's attached to. Because the Script is added to multiple GameObjectes with different values I have to call a Function in It without using anything than the unused Script.
Hope someone can help me with that ;)
Your sincerely,
Pwnographyz
There are plenty of tutorials about this. You're going to want GetComponent, FindObjectsOfType, or some mix of both.
If you have a more specific question, please feel free to resubmit; otherwise, maybe try the forums or Google.
As far as I know to call a function from another script you need some kind of reference if the script isn attached to the same object. If you want to reference the script only, try
var script:ScriptName
function Start(){
script = GameObject.Find(Object the script is attached to).GetComponent("ScriptName") as ScriptName;
}
Answer by Kiwasi · Jun 11, 2014 at 09:15 AM
For time 752. Seriously, use Google or something. Not sure why a mod let this through.
List of ways to access another script
Use Find and GetComponent. Do this once and store the reference until you are done with it. Fastest method, should be used as default unless there is a good reason not to.
Use SendMessage. Best in cases where there might be more or less then one receiver on the target gameObject
Use static methods. Best for things like game managers and helper classes that don't change.