How to enable parts / components in Unity C # only with game object in script

I use Photon to create a multiplayer game in my game, to ensure that one player does not control them all, when you appear on the client side, he activates your scripts / camera so that you can see and move around.

Although I can’t think of a way to solve this problem, since I don’t know how to enable / disable child components or enable a child child.

I want to enable this through scripts http://imgur.com/ZntA8Qx

and this is http://imgur.com/Nd0Ktoy

My script:

using UnityEngine;
using System.Collections;

public class NetworkManager : MonoBehaviour {

public Camera standByCamera;
// Use this for initialization
void Start () {
Connect();
}

void Connect() {
Debug.Log("Attempting to connect to Master...");
PhotonNetwork.ConnectUsingSettings("0.0.1");
}

void OnGUI() {

GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
}

void OnConnectedToMaster() {
Debug.Log("Joined Master Successfully.");
Debug.Log("Attempting to connect to a random room...");
PhotonNetwork.JoinRandomRoom();
}

void OnPhotonRandomJoinFailed(){
Debug.Log("Join Failed: No Rooms.");
Debug.Log("Creating Room...");
PhotonNetwork.CreateRoom(null);
}

void OnJoinedRoom() {
Debug.Log("Joined Successfully.");
SpawnMyPlayer();
}
void SpawnMyPlayer() {
GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("Body", Vector3.zero, Quaternion.identity, 0);
standByCamera.enabled = false;
((MonoBehaviour)myPlayerGO.GetComponent("Movement")).enabled = true;

}
}

monobehaivour - , , , -, , , , , .

, , , , , , myPlayerGO Game, .

, , , , .

, , , .

+4
2

I, gameObject.GetComponentInChildren

ComponentYouNeed component = gameObject.GetComponentInChildren<ComponentYouNeed>();
component.enabled = false;

gameObject.GetComponentsInChildren

+1

, , .

, , - script, HeadMovement script Body. :

public class BodyController : MonoBehaviour
{
  public HeadMovement headMovement;
}

, :

BodyController bc = myPlayerGo.GetComponent<BodyController>();
bc.headMovement.enabled = true;

GetComponentsInChildren():

HeadMovement hm = myPlayerGo.GetComponentsInChildren<HeadMovement>(true)[0]; //the true is important because it will find disabled components.
hm.enabled = true;

, , . HeadMovements, . prefab , , .

+1

All Articles