- Home /
How to call a function in another script
I have 2 scripts (Javascript, by the way), scriptA and scriptB. By searching in unity's answers forum, I learned how to call a function in scriptA that is inside scriptB. Like this:
ScriptA
var scriptB: ScriptB;
scriptB.Test();
ScriptB
function Test() {
//code
}
This code works fine, but i need a to call a function with inputs and outputs. For example, if I use this for ScriptA and ScriptB, I get an error:
ScriptA
var scriptB: ScriptB;
print(scriptB.Test(2,3));
ScriptB
function Test(a : int,b : int):int {
return a*b
}
I don't know what the problem is, and I can't find any help on google, unity community, or anywhere else. Can someone show me how to call a function from another script with inputs and outputs?
Answer by Monko · Jul 02, 2013 at 04:48 PM
After lots more searching, I did finally come up with a solution!!!!!
The variable scriptB wasn't never defined, so I decided to do this:
Script A:
var scriptB: ScriptB;
var voxelCube : GameObject;
voxelCube = GameObject.Find("VoxelCube");//this is the name of the object
//that contains script B`
scriptB= voxelCube.GetComponent(Scr_World);
print(scriptB.Test(2,3));
Script B:
function Test (a : int, b : int)
{
print (a*b);
}
This makes it possible to call a function located in Script B from Script A, including the inputs and outputs of the function.
Answer by Jessy · Jul 01, 2013 at 07:55 PM
You should have posted your error, but regardless of whether this solves your problem, don't name your variable the same as the class. The compiler is probably telling you that Test isn't a static method. Capital letters are not used for fields. Some people will tell you that you have the freedom to do that, but it shows no respect for other coders. It ought to be disallowed by the compiler.
Understood. I edited my code above. Also, here is the error message it gives me:
Object reference not set to an instance of an object.
Look at ScriptA in the inspector. You see where it says scriptB None (ScriptB)
? That is what the error message means. You need to drag the object with ScriptB on it to that slot in the inspector; or else have Script A find and assign it in Awake().
Answer by create3dgames · Jul 01, 2013 at 07:54 PM
Maybe something like this:
ScriptA
function Start ()
{
BroadcastMessage( "Test", 2, 3);
}
ScriptB
function Test (a : int, b : int)
{
print (a*b);
}
I tried it, and it doesn't work. Also, I don't even know what Broadcast $$anonymous$$essage does.
Anyone else have any suggestions?
Look up one of the thousands of pages that explain why you get "Object reference not set to an instance of an object."
It's solved, see above. The reason Jessy was getting snippy with you is that this has nothing to do with your particular problem, and nothing to do with the title of your question; it's just that you don't know how to assign references in the inspector, which is one of the first things you need to know to use Unity; which is why they put it in the first section of the manual.
Your problem is not that you have inputs/outputs, it's that you have a null reference. scriptB is equal to 'null'; you have to set it to something before you can use it.
@$$anonymous$$onko What do you mean, you tried it, and it "doesn't work". I've never heard such a thing. Can you be a little more descriptive? Did you get any errors? And if you don't know what Broadcast$$anonymous$$essage
is then look it up. Trust me, it works perfectly.