Stupid Question - Destroying child gameobjects

Howdy!

I have the following code…

using UnityEngine;
using System.Collections;

public class PlayerScript : MonoBehaviour {

	// Runs when connected to the server
	void OnNetworkInstantiate(NetworkMessageInfo msg )
	{
		if(networkView.isMine)
		{
		}
		else //If this isn't our own player turn on controllers etc
		{
			Destroy(GetComponent<MouseLook>());
			Destroy(GetComponent<FPSInputController>());
			Destroy(transform.FindChild("Main Camera").GetComponent<MouseLook>());
			Destroy(transform.FindChild("Main Camera").GetComponent<GUILayer>());
			Destroy(transform.FindChild("Main Camera").GetComponent("FlareLayer"));
			Destroy(transform.FindChild("Main Camera").GetComponent<Camera>());
			Destroy(transform.FindChild("Main Camera").GetComponent<AudioListener>());
			
		}
	}
}

How would I go about finding and destroying a child gameobject? Whenever I try I get the following error…

“Can’t destroy Transform component. If you want to destroy the game object please call ‘Destroy’ on the game object instead”

Thank you :slight_smile:

Why not just destroy the camera itself? It seems like you’re already trying to destroy everything attached to the camera. Plus a child is something attached to an object, such as if I had cube 1 and cube 2, then I dragged cube 2 onto cube 1 then that would be a child of that object. Those objects attached to the camera itself are not the same thing. You should try disabling that stuff instead or I’m not sure if there’s a remove component from an object through code.

That was a very handy tip but it still doesn’t rectify the issue I’m having.

I should have probably explained myself clearer. I have a secondary camera attached to my main camera which is used for rendering flashlights and the avoidance of clipping. The follow screenshot shows the structure…

1216088--49710--$flashcamera.png

How would I reference and destroy the child camera?

Thank you for the tip and my apologies for not being clearer.

Simplest way, give it a tag, then find by tag
less simple way, find components in children, then just destroy the compoentns gameObject, if it isn’t Camera.main