Access other script variable in same object

I have the following array set up in one class, (DrivingManager), in an object:

public Color[] CarColours = new Color[10];

In another class, (CarColor), in the same object, I have a similar array declared like this:

private Color[] availableCarColours = new Color[10];

I want to copy the contents of CarColors to availableCarColors. I have tried the following:

availableCarColours = gameObject.GetComponent<DrivingManager>().CarColours;

and

DrivingManager tempscript = gameObject.GetComponent<DrivingManager>();
    availableCarColours = tempscript.CarColours;

But with no luck. The error I get is “NullReferenceException: Object reference not set to an instance of an object
CarColor.Start ()”

I’m not sure what I’m doing wrong here. I have checked that both arrays are declared and is working in their respective classes.

Ah ok. This one is a little more involved than you might like. You need to send a message to the other script. Since they are on the same object it will be easy.

First when you want to copy place this into your DrivingManager script.

gameObject.SendMessage("functionname", CarColours, SendMessageOptions.DontRequireReceiver);

What this will do is send a message to the scripts on the gameObject to use function “functionname” and the input to functionname will be CarColours.

Secondly add this function to your other script add the following function:

   function functionname ( arr : Array ) {
    	availableCarColours = arr;
    }

This will activate when the message is sent and update availableCarColours with the information that biggybacked its way with the message. arr will absorb the input of CarColours from the message and make it a useable variable. (IE the message sets arr = CarColours) I seem to remember there being an issue with sending arrays… if so, I can show you some simple code to pack it as a string, send it, and decode it, if you can’t figure that one out on your own. ^.^ I hope this helps!