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).
?
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.
?
Then I Run and play,When the function CellPickup() is called,something wrong happen:
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. [1]: /storage/temp/76420-j1.jpg [2]: /storage/temp/76422-j2.jpgUnassignedReferenceException: The variable collectBox of playerCollisions has not been assigned.
Answer by xiaotianhu · Aug 22, 2016 at 04:28 AM
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.
Answer by ScaniX · Aug 19, 2016 at 11:52 AM
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.
If I get the reference by using GameObject.Find("charge$$anonymous$$eter1"),then I get a instance of GameObject,not a instance of Renderer,so when I do collectBox.material.mainTexture ,it will fail with "material' is not a member of 'UnityEngine.GameObject".So what should I do? PS:I tried with GameObject.Find("charge$$anonymous$$eter1") as Renderer but I got Null.
Your answer
![](https://koobas.hobune.stream/wayback/20220612091128im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why i can't see scenes in inspector? 0 Answers
How do I use EditorGUILayout.EnumPopup with an enum with 'holes' in a custom inspector. 3 Answers
Unassigned Reference Exception on an Animator, but Animator is assigned in Inspector? 0 Answers
Unity 4.1.5 is not showing certain public variables in the inspector 1 Answer
Inspector List: DropDown list Update regarding Prefab in asset folder 1 Answer