- Home /
communication between scripts
I can't know the way how to communicate between the scripts. I try to change variable A on JavaScript A at JavaScript B(CScript B). But I don't know how to change it. How can I do it? Also I want ask one more question. Can I communicate between C Script and Java Script? Then How?
it is difficult to communicate between c# and java script you can only delay the execution but you can easily communicate between c# and c#
java script and java crip
Answer by GuyTidhar · Sep 13, 2011 at 07:03 AM
There are several things to take in account in order to understand the answer to your question.
A script is a set of rules you define.
In order for these rules to exist in the computer's memory when you need them you can either make make an instance of them (by declaring a variable of the type of script you defined) or declare some of the definitions within this set of scripts as static (functions or variables within the script you wrote). When you declare something as static, it means it will have one instance in memory and each time you call that static variable or function you use that exact instance (you can not make an instance of a static object).
In order to access a variable in script A from script B, you would usually do the following: Lets assume both scripts are javascript:
1) Make a 2 new javascript file: ScriptA.js ScriptB.js
2) In ScriptA, lets say you want an integer defined. You would write within ScriptA:
var myInteger : int = 0;
3) In ScriptB, lets say you wish to use this integer:
var my1ScriptAinstance : ScriptA = null;
var my2ScriptAinstance : ScriptA = null;
function Start()
{
// Create 2 instances of ScriptA in memory
my1ScriptAinstance = new ScriptA();
my2ScriptAinstance = new ScriptA();
}
function Update()
{
// Each time Update is called, print and then increase myInteger in my1ScriptAinstance by 1
Debug.Log("My 1st integer = " + my1ScriptAinstance.myInteger++);
// Each time Update is called, print and then decrease myInteger in my2ScriptAinstance by 1
Debug.Log("My 2nd integer = " + my2ScriptAinstance.myInteger--);
}
You can also have one or both of these scripts defined as C# (though you need to change the code for C#). In order for that to work you need to consider when each of these scripts get compiled. It depends on where you actually place the C# script and the JS script - the script X you which to access from script Y, must be compiled before script Y, or else script Y won't understand what you are referring to (compilation is the process of creating the actual machine code from the human language set of rules you defined in the JS or C# script). In order to understand more about compilation order in Unity, check this out: http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html
NOTE: you also have the option to learn to use the SendMessage (http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=SendMessage). I highly recommend against it, as it involves in making your code work much slower, unless you use it in very specific places.
Answer by robert_mathew · Sep 13, 2011 at 07:04 AM
http://forum.unity3d.com/threads/21098-Calling-c-from-JS-and-vice-versus
http://forum.unity3d.com/threads/19800-How-do-you-communicate-between-scripts
http://answers.unity3d.com/questions/140335/communicate-between-two-bools-c.html
http://unity3d.qatohost.com/questions/18281/trying-to-set-a-javascript-variable-from-c-sharp.html
Your answer
Follow this Question
Related Questions
Invalid rank specifier error 0 Answers
How to make the trigger work only once. (SOUND) 1 Answer
Add a Rigidbody to prefab using script 3 Answers
Best way to create a train. 0 Answers
Creating A wall Jump 0 Answers