[JS] How to call a function from another script.

Hello Guys,

Can anyone tell me, how to call a function from another script. I have 2 objects. First the enemy, second am empty gameobject as “gamecontroler”.
When the enemy touch my player, my enemy should call the Game_over() function which is attached in a seperate script at the gamecontroler object.

I know I can insert the Game_over() function into the enemy script, but thats not what I want. How to call variables form another script is no problem for me, but how to do this with a function, I have no idea.

Many thank in advance.

gamecontroler.GetComponent(myScript).myFunction();

OK! Thats easy!
Many thanks! You made my day :smile:

That may make error if i’m not wrong.

var aScript : myScript = gamecontroler.GetComponent(myScript);
if (aScript)
aScript.myFunction();

is safer, isn’t?

Definetely that is the safe way.

I get gamecontroler is an unknown identifier…

“gamecontroler” is a reference to a GameObject in Maglat’s game that has a “myScript” attached to it. You would reference some GameObject that’s in your game, Jfhutchi.

(FYI, you typo’d gamecontroller Maglat :))