- Home /
Accessing functions in other scripts.
In Javascript, I have tried the method listed here...
http://answers.unity3d.com/questions/32/how-do-i-call-a-function-in-another-gameobjects-script
...but following it got the error: Assets/HazardStatic.js(18,23): BCE0019: 'Test' is not a member of 'UnityEngine.GameObject'.
The scenario is thus: I have two scripts named Testing and HazardStatic both assigned to the same game object named HazFlowerCol.
In the Testing script are a static var named "test" and a static function named "Test".
I can access and change the var "test" but cannot trigger the function "Test".
I will vote answered to the answer that leads to my HazardStatic script being able to call the Test function in Testing. Please be explicit in your JavaScript answer as I have already been through everything I could on this page without success:
http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
to no avail.
Thanks!
--Goody!
And I may even upvote you if you can do it without using the words "simple" or "simply". :)
Answer by Jaap Kreijkamp · Dec 08, 2009 at 02:22 AM
EDIT: added example with static and nonstatic functions/vars
If you use the method as in the docs you mentioned, you have to connect the script ScriptB to the other property of ScriptA. You do this by drag/dropping the gameobject self onto the property other of ScriptA in the inspector. For static functions and vars you refer them using the scriptname, e.g. ScriptB.staticVar or ScriptB.StaticFun().
Examples:
ScriptA
var other : ScriptB;
function Update () { other.testValue = 1; other.Test(); other.staticTestValue = 2; ScriptB.StaticTest(); }
ScriptB
var testValue : int = 0; static var staticTestValue : int = 0;
static function StaticTest() { Debug.Log("Static Test: " + staticTestValue); }
function Test() { Debug.Log("Test: " + testValue); }
Wonderful! Thank you so much for the script example! It makes testing it out and fiddling with it $$anonymous$$UCH easier! I'd tried the method of selecting an object but not drag/dropping and with your concise directions it worked great.
Dang! I cannot upvote yet (I tried but it says I need 15 Unity$$anonymous$$agicPoints to do so) but I would if I could. :)
Somehow I now have 33 $$anonymous$$agicHappyPoints! Upvoted!
Your answer
![](https://koobas.hobune.stream/wayback/20220613180441im_/https://answers.unity.com/themes/thub/images/avi.jpg)