- Home /
talk between C# scripts
I have 2 C# scripts: USend.cs and UReceive.cs
USend.cs has a variable I need to reference: public bool[] BHarray = new bool[128];
I wand UReceive to reference BHarray. How is this done?
In Unity, I have dragged both C# scripts onto the same "Empty" object.
Thanks, Jake
Answer by skovacs1 · Dec 01, 2010 at 07:09 PM
You can get a reference to the instance of the script and the instance of the variable is public from there.
In UReceive, add this:
USend script = GetComponent<USend>();
//where you want to use BHarray script.BHarray[foo] = bar;
You could alternatively make BHarray static so that there is only one of it and you don't need a reference:
change BHarray to:
public static bool[] BHarray = new bool[128];
and in UReceive where you want to use BHarray:
USend.BHarray[foo] = bar;
If your accesses are uni-directional, you could also alternatively use SendMessage, sending an approrpiate object along with it.
Side Note (just a thought):
128 bools? You don't give your use case, so I can't say if it is right or wrong, but maybe separating them out into separate unsigned ints and doing some bit masks might be more reasonable. If any of the bools are exclusive, you might consider enums as well. Tracking down issues with an array of 128 bools becomes hard to follow and potentially unmanageable. Good luck.