- Home /
calling a function from another script
Hi,
I have two scripts attached to one prefab I'm trying to call a function in one of them from the other:
In the caller
var rightLines = CheckRight.checkRight(X, Y);
in the other script named CheckRight.js:
function checkRight(X, Y){
...
return rightLines}
Any help much appreciated :)
Answer by syclamoth · Nov 11, 2011 at 01:05 PM
If you have a reference to the gameObject on which your 'CheckRight' script is a component, just use
checkRightGameObject.GetComponent(CheckRight).checkRight(X, Y);
If they're both on the same object, you just need to use
GetComponent(CheckRight).checkRight(X, Y);
Answer by JULike · Nov 11, 2011 at 02:42 PM
Thanks,
Both scripts are attached to the same object called 'tile', do I need to explicitly reference 'tile' from one script to access its functions from the other script, if so how?
Kind Regards
AAAND you doubleposted. This is why it's easier to just use comments- they don't have to go through the moderation queue!
Thanks,
Script1 GetComponent(CheckRight).checkRight(adjustedX, adjustedY);
Script2 (CheckRight.js) function checkRight(adjustedX, adjustedY){}
Ok, so both are attached to the same game object I just checked in the inspector. And I'm getting $$anonymous$$ethod not found exception, when I double click on it it takes me to the attempted call in Script1
Soz about double posting :/
Thanks
Hmm, I'm not so familiar with JS- it's possible that you need to use
var checkRight : CheckRight = GetComponent(CheckRight);
answer = checkRight.checkRight(X, Y);
Thanks,
Same $$anonymous$$issing$$anonymous$$ethodException $$anonymous$$ethod not found: 'CheckRight.checkRight'.
Did you make the function a public function? I always get confused by the fuzzy syntax in JS, but that might be the problem (unless it's one of those things that magically work anyway).