assign variable by inspector failed...

Hi guys,not good at english, but really need some help.
I’m learning with the book http://unitybook.net/,and got stuck.Searched for a day but nothing helps.

so here is my question:
I have a generator(like a box) in Scene,and I want to change it’s texture by script(JS).

?alt text

my script is on the camera,which is the FirstPersionCharacter.Part of it is this:

var collectImage:Texture2D;
public var collectBox:Renderer;

function CellPickup() {
charge ++;
collectBox.material.mainTexture = collectImage[charge];
Debug.Log(collectBox);
}

When the function CellPickup() is called,I want to change the collectBox’s texture.the collectBox variable represents chargeMeter1 ,so I assigned this variable by inspector ,by drag the chargeMeter1 from Hierarchy.

?alt text

Then I Run and play,When the function CellPickup() is called,something wrong happen:

UnassignedReferenceException: The variable collectBox of playerCollisions has not been assigned.
You probably need to assign the collectBox variable of the playerCollisions script in the inspector.

But I have already assigned the collectBox variable by inspector.So Please ,anything will be helpful.Thank you.

As there is an “apply” button, I guess those objects are prefabs.

You cannot assign an actual scene instance to a prefab. That will be gone when you instantiate the prefab again. At least I experienced that behaviour in my scenes and it kinda makes sense. I don’t think you can assign a member from one prefab to another one either.

You will have to assign the reference in the Start() method by using GameObject.Find(“chargeMeter1”) in case of dynamically created things.

Thanks @ScaniX, And I have solved this.

I do get the reference like this:
var cb:GameObject;
cb = GameObject.Find(“chargeMeter1”);
collectBox = cb.GetComponent(Renderer);

The GameObject is the pack of all objects in Unity,and one object can have many components on it,is that right?

Then I also found something else,which may cause the problem.

Science I use the First Controller Camera,I gets two things in my Hierarchy:
Parent is FPSController,and Child is FirstPersonCharacter.
I’v put my script compoment on both of these,and then I only remember to change the variable with the script on the child FirstPersonCharacter.So when I Debug in the function Start(), each time I’ll get two results.Then after I tried several minutes,I found only the script on parent FPSController will work.
Then I do
collectBox.material.mainTexture = collectImage[charge];
Everything goes right.