- Home /
Calling functon attached to another object (Javascript)
I have a very simple scene with two cubes (CubeA and CubeB)
Each cube has its own java script file attached to it (ScriptA and ScriptB).
Here's what I have so far
//ScriptA
ScriptB.Test();
//ScriptB
public static function Test() { print("Test called!"); }
I'd rather do something like this
//ScriptA
CubeB.Test();
Is anything like this possible?
Already asked and answered: http://answers.unity3d.com/questions/2395
Answer by Loius · Nov 27, 2010 at 07:43 AM
A couple of ways -
var component : ScriptB; // assign in inspector
component = targetThing.gameObject.GetComponent( ScriptB ); // get from something you already have ("targetthing")
component.Test(); // call function
Answer by azzogat · Nov 28, 2010 at 09:39 AM
There are at least 2-3 more identical questions posted on this matter yesterday.
For a more detailed explanation you can check the answers for this question:
http://answers.unity3d.com/questions/29163/getcomponent-keeps-returning-null
Your answer
![](https://koobas.hobune.stream/wayback/20220613084718im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Set variable in script from a different script... 3 Answers
Disable/Enable Script or Add/Remove? 1 Answer
Change position of GameObject between unity3d and eclipse 0 Answers
How to change variables on a new gameobject's script 1 Answer
gameObject.GetComponent("Script").enabled = true not working 5 Answers