x


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

more ▼

asked Dec 01 '10 at 07:01 PM

user-6981 (google) gravatar image

user-6981 (google)
1 1 1 1

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

1 answer: sort voted first

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.

more ▼

answered Dec 01 '10 at 07:09 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

(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:

x83
x53

asked: Dec 01 '10 at 07:01 PM

Seen: 1318 times

Last Updated: Dec 01 '10 at 07:24 PM