x


Call a function in a different script, using javascript.

How can i in script A call a custom function in script B, using javascript?

more ▼

asked Feb 08 '10 at 11:29 AM

Mattivc gravatar image

Mattivc
1.7k 55 60 67

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Try this.


Script A

var ScriptB : ScriptB; //This should be were Script B goes

function Update () {
    ScriptB.Test();
}


Script B:

var test : int = 0;

function Test () {
    test++;
}


In this example Script A tells Script B to add 1 to the 'test' integer ever frame.

more ▼

answered Feb 08 '10 at 12:10 PM

AquaGeneral gravatar image

AquaGeneral
287 3 4 20

So the syntax is: "nameOfOtherScript.nameOfFunction();" ?

Feb 08 '10 at 12:51 PM Mattivc

Spot on. With this approach it is in theory (at least) quicker than the GetComponent method, as it doesn't have too look for anything on the initial run.

Feb 08 '10 at 01:00 PM AquaGeneral
(comments are locked)
10|3000 characters needed characters left
  1. You can use Component.GetComponent. But it will work only if the target script is attached to your object or you need to have reference to another object to invoke GetComponent on it.

  2. You can create static methods in your script and invoke it from anywhere .

more ▼

answered Jan 01 '12 at 04:33 PM

SergeyLavren gravatar image

SergeyLavren
1

(comments are locked)
10|3000 characters needed characters left

Use the function GetComponent() to find a script attached to a GameObject.

Reference and Examples

more ▼

answered Feb 08 '10 at 12:48 PM

efge gravatar image

efge
5.1k 5 14 38

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5072
x108

asked: Feb 08 '10 at 11:29 AM

Seen: 15698 times

Last Updated: Jan 01 '12 at 04:33 PM