Changing child objects' materials

I am trying to change the texture of an object’s child object when the main object gets clicked on. I tried using the script below but received the warning “NullReferenceException” and “UnityEngine.Component.get_renderer ()”. So I am doing something wrong, but I don’t know what! Here’s the script. Any ideas?

transform.Find(“child”).renderer.material.mainTexture = texture;

Should work…the child object is literally called “child”? If not then either change its name to “child” or use its actual name in the script. And texture is declared like “var texture : Texture2D” elsewhere, and has a texture dragged onto it in the inspector?

–Eric

You’re right. It should work - it does. I was using multiple objects and confusing the ones I was testing on. However, I didn’t have the “2D” on “Texture” and added it. Is it needed? Is it different from just “Texture”?
Thanks for you help!

Good question…I always use “Texture2D” as the type, but I just tried “Texture” and they seem to do the same thing.

–Eric

Texture is more general. For example you could also assign a RenderTexture to it or a CubeMap. May or may not be what you want.