- Home /
How to enable children of child game object?
I'm following Quill18's Multiplayer FPS Tutorial and in Part 5 we have to enable some components in a instantiated player prefab. However, in my particular case, the player itself is a child of another GO (caller Player GO) and there's two cameras (Camera1 set to depth only and Camera 2 set to skybox) that are children of the child.
I cant enable the 2 cameras gameobjects no matter what I try. Help?
There below is my hierarchy for the player GO...
and the script that instantiates players:
using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour {
public GameObject standbyCamera;
private GameObject player;
private GameObject camera1;
SpawnSpot [] spawnSpots;
// Use this for initialization
void Start () {
spawnSpots = GameObject.FindObjectsOfType<SpawnSpot> ();
Connect ();
}
void Connect () {
PhotonNetwork.offlineMode = false;
PhotonNetwork.ConnectUsingSettings ("RPGVR 1.0.0");
}
void OnGUI (){
GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString() );
}
void OnJoinedLobby() {
PhotonNetwork.JoinRandomRoom ();
}
void OnPhotonRandomJoinFailed() {
PhotonNetwork.CreateRoom (null);
}
void OnJoinedRoom(){
Debug.Log ("OnJoinedLobby");
SpawnMyPlayer ();
}
void SpawnMyPlayer (){
if (spawnSpots == null) {
Debug.LogError ("No Spawn Spots avaiables. Don't Panic");
return;
}
SpawnSpot mySpawnSpot = spawnSpots[ Random.Range (0, spawnSpots.Length) ];
GameObject myPlayerGO = (GameObject) PhotonNetwork.Instantiate("Player GO", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
//Disable the Standby Camera
standbyCamera.SetActive(false);
//Enable "Sky Dome" Child of "PlayerGO" Game Object
myPlayerGO.transform.FindChild("Sky Dome").gameObject.SetActive(true);
//Enable Components in "Player" Children of "PlayerGO" Game Object
//The camera code below does not work =(
camera1 = GameObject.FindWithTag("MainCamera");
camera1.SetActive(true);
//Below code is okay
try
{
player = GameObject.FindWithTag("Player");
player.GetComponent<MouseLook>().enabled = true;
MouseLook mouseScript = myPlayerGO.GetComponentInChildren<MouseLook>();
if(mouseScript != null)
{
mouseScript.enabled = true;
}
CharacterMotor motorScript = myPlayerGO.GetComponentInChildren<CharacterMotor>();
if(motorScript != null)
{
motorScript.enabled = true;
}
FPSInputController controlScript = myPlayerGO.GetComponentInChildren<FPSInputController>();
if(controlScript != null)
{
controlScript.enabled = true;
}
}
catch
{
Debug.Log ("Error finding children");
}
}
}
Answer by ramp · Jan 01, 2015 at 09:24 AM
Hello,
First of all you have to Make children variable as private to public and assign from Hierarchy.
public GameObject player;
public GameObject camera1;
Sorry but can you explain a little more? I made the children var public and assigned from hierarchy, however the player must be instantiated into the scene so, even if I assigned the game objects correctly, they vanish as soon as I delete the player from the scene (because he's suposed to be a prefab instantiated at runtime).