- Home /
Enable Child Component C#
Hello.
How would i go about enabling a child camera component?
My player looks like this in hierarchy
Player
PlayerCamera
WeaponCamera
I need to enable the Camera component on both camera's
void OnJoinedRoom()
{
GameObject player = PhotonNetwork.Instantiate("Player", Vector3.zero, Quaternion.identity, 0);
//Camera
PlayerCamera camera = player.GetComponentInChildren<Camera>();
camera.enabled = true;
//Player
CharacterController characterController = player.GetComponent<CharacterController>();
characterController.enabled = true;
It is right to use GetComponentInChildren isn't it? Thanks.
Comment
Just to be clear, This is for $$anonymous$$ultiPlayer, Thanks.
Best Answer
Wiki
Answer by AlkisFortuneFish · Jan 17, 2014 at 09:52 PM
GetComponentInChildren will only get you the first camera component it finds, what you want is GetComponentsInChildren, which will return every camera component in the object's hierarchy. You can then loop over them to enable the one(s) you want. Make sure you set the includeInactive argument to true, otherwise it will defeat the purpose of the whole thing!
Camera[] cameras = (Camera[]) GetComponentsInChildren(true);
for (int i = 0; i < cameras.Length; i++)
{
cameras[i].enabled = true;
}