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?
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.
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.