Accessing a child's renderer

Hello,

I’m trying to write a script for a nested set of objects that need to appear and disappear when the function setVisibility is called (from outside the script). The script in question is attached to the parent object, which has a child “cameraShape.” I thought the easest way to have all the objects disappear from the screen would to store a reference to the child’s renderer during Start(), then access when the setVisibility() function is is called.

However I can’t seem to find the child’s renderer. I continually get a nullReference Error. I’ve looked around the forums and tried solutions to similar problems but nothing works.

Here is my code:

private var cameraTransform : Transform;
private var cameraRenderer : Renderer;

function Start () {
	cameraTransform = transform.Find("cameraShape");
	cameraRenderer = cameraTransform.gameObject.renderer;
	print("stalk ready");
}

function updatePosition (newPositions : Array) {
		cameraTransform.localEulerAngles = Vector3(newPositions[1],newPositions[0],0);
}

function setVisibility (amIVisible : boolean){
	renderer.enabled = amIVisible;
	cameraRenderer.enabled = amIVisible;
}

Any thoughts or examples of accessing a child’s renderer would be greatly appreciated!

Thank you.

Are you saying that the rendered item(s) are underneath cameraShape? If so, you might like to try GetComponentsInChildren/GetComponentInChildren.