4

I am using Photon to make put multiplayer in my game, to ensure that one player doesn't control them all, when you spawn in, client side it will activate your scripts/camera so you can see and move.

Although I can't think of a way around this problem, since I don't know how to enable/disable children's components or enable a child's child.

I want to enable this through scripting https://i.stack.imgur.com/TmhV2.jpg

and this https://i.stack.imgur.com/JqEAt.jpg

My script is this:

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;

}
}

The bit at the bottom underneath the thing with monobehaivour is where I want to enable them As you can see I have already figured out how to activate something that is part of the game object I spawned, I just need help with what I said above, thank you for your help.

I'm spawning it through a prefab, so I want it to edit only the one I spawn, and not every other one in the level, as in I want to enable these components using the myPlayerGO Game object, and that one only.

This is all I need to get my Game working, so please help.

If this is a duplicate, I'm sorry because I wasn't sure how to word the title of this.

user3071284
  • 6,955
  • 6
  • 43
  • 57

2 Answers2

1

I unity you can enable and disable components in child object by using gameObject.GetComponentInChildren

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

Also can use gameObject.GetComponentsInChildren

Nain
  • 1,204
  • 1
  • 12
  • 17
  • Thankyou I do not know who answered first between you and Chris, but I fixed the imgur links so you will be able to see the other half of the problem –  Aug 13 '15 at 19:48
  • if a game object is not active you cannot activate it by getting its component or if i say that you can not even get those components so what you need to do is to create a public reference of your camera in your HeadMovement script and then user cameraRef.setActive(true) to activate . – Nain Aug 13 '15 at 22:15
1

Both image links go to the same place but I think I understand.

I would probably recommend that you place some kind of script that allows you to wire up the HeadMovement script in the Body game object. For example:

public class BodyController : MonoBehaviour
{
  public HeadMovement headMovement;
}

Then you can wire this up in your prefab and then call:

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

Another fix would be to use GetComponentsInChildren():

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

I would probably say the first is a better option because it is more explicit and also faster. If you have multiple HeadMovements then you will run into problems. It also requires a crawl of your entire prefab hierarchy just to find something that you already knew the location to at compile time.

Chris Hill
  • 1,914
  • 1
  • 16
  • 24
  • I am trying these as we speak, also fixed the imgur links, you should be able see my other problem now, –  Aug 13 '15 at 19:35
  • I can't do the first one, as I have already tried it before. But the GetComponentsInChildren passed with flying colours, I would make this the best answer, but I had to fix the imgur links for you to be able to see the other half of the problem. –  Aug 13 '15 at 19:46