Sorry if the title is not TOO specific, I’ll try to describe the whole process that led to my question, excuse possible mistakes in terminology, I’m fairly new to Unity, still experimenting.
I wrote a simple script to change a color of a GameObject via a GUI Button.
var cubeRenderer:Renderer;
var myStyle:GUIStyle;
function OnGUI()
{
if(GUI.Button(Rect(0,0,100,50),"Click Me!",myStyle))
cubeRenderer.material.color = Color.red;
}
I created an empty GameObject and attached the script to it. Then I would create a GameObject like a cube, I would drop this Cube to the exposed cubeRenderer variable slot in the Inspector and it would work like a charm.
After that I needed to experiment with scripted-controlled animation.
I created 2 simple boxes in 3DS Max with a simple animation like doors opening and closing. I exported that to .FBX, imported it to Unity and wrote a script to animate it when I press specific buttons. I attached the script to the PreFab in the hierarchy tab and it all worked out fine.
And now we come to my issue. I wanted to try and combine the 2, that is, opening and closing the doors with keypresses AND change their color with the push of the button.
The thing is, that my imported GameObject consists of 2 separate objects, 2 doors, combined in a Prefab inside Unity. I can only drag n drop one of them the “cubeRenderer” slot in the Inspector. Of course I could add a second exposed variable of type Renderer to my colorchanging script, but I was wondering if I could drag n drop the whole Prefab (include both doors that is), and then use the script to change the color of both gameobjects included in the Prefab.
I tried to alter the the script, using an exposed variable of type GameObject and then dragging the whole Prefab into the script, but when I hit play, while the animation works just fine, when I try to change the color, I get a
NullReferenceException: Object reference not set to an instance of an object
That’s my new script
var doors:GameObject;
var myStyle:GUIStyle;
function OnGUI()
{
if(GUI.Button(Rect(0,0,100,50),"Click Me!",myStyle))
doors.material.color = Color.red;
}
I can understand why it doesn’t work, it probably need a Renderer variable, how can I change the script to use both objects at once?
Sorry for the long post, hope my question is clear enough