- Home /
Question by
uberokeer · Jul 16, 2013 at 02:33 PM ·
javascriptvariablereference
Unity3d Javascript Reference Script Variable
Hello, what I was wondering was if I was able to reference a script, or make a script variable for instance:
public var scriptRef : script;
or something like that so that I don't always have to use gameObject.GetComponent? Because that doesn't always work for Me.
Comment
Best Answer
Answer by amphoterik · Jul 16, 2013 at 02:37 PM
Yes you can. Just replace the word "script" with the actual script name. Then, in the editor just drag over the object with the script attached into the newly formed property in the inspector view:
public var scriptRef : MyScriptType;
If there is no scene object with the script (if you are instantiating it for instance), you will need to call GetComponent().
If your question instead is how to save a local script component, then you would do:
public var scriptRef : MyScriptType;
function Start()
{
scriptRef = GetComponent(MyScriptType);
}
This way you only ave to do it once at startup