Is there a way in JS to have multiple variables of same types in different script connected, like to a same pointer so when one variable changes, automatically the others reflect the same change?
Surely you just want one variable in this case? I’d suggest using a static variable; that way every script can easily access it.
–Eric
i’d rather keep this contained to the few scripts that use that connection, because there are many objects that have this sub-structure and doing it with statics would require to store an array and connect them etc… I’d like to avoid global stuff for now.
I know within a script if your to
var t : Transform = transform;
var tt : Transform;
tt = t;
when you change tt it changes t (I think)
I’d like to do the same thing between scripts.
I second the static method -
You can always have a static class, although I think you can’t extend monobehavior.
then your access to the variable is trivially easy, no connections required.
-in C#
public static class SharedData(){
public static int XYZ = 0;
public static Transform blah;
}
then just access it anytime, SharedData.XYZ… No connections or arrays required, unless we aren’t understanding your case correctly.
say you have 10 roots composed each of the same 4 GO
each one has the scripts that I want communication in between
but
I only want the 4 GO of each root to communicate
not the 40 GOs
with the static class or variable all 40 of them share the same data
My inclination would be to have 1 script control all 4 GOs.
I can’t foresee a problem there, but maybe there is.
You could build a messaging system if you absolutely must design it this way. Then you just pass messages around
(rootId, variableToChange, value) - assuming each GO knows which root it belongs to.
Personally I hate messaging. I’d figure out a way to have one script control them all, if i could.
Otherwise, a static variable for each root.
In JS on some empty…
Controller.js
static var myVar : Type;
function Update()
{
myVar = ...do stuff
}
on your game objects…
myObjects.js
var myGOVar : Type;
function Update()
{
myGOVar = Controller.myVar;
}
though this way myObjects.js can only be assigned to the 4 you want to communicate or you need to implement some other way to differentiate. how are you defining the root? there is stuff like transform.root
so maybe then…
transform.root = myGOVar;