I can't get the camera component from inside the script !

The thing is i’m making a multiplayer game and to avoid players having components mix up i have to disable them in the prefab and when i instantiate each one of them i enable all of his components , and i have a child called “Main Camera” , when i try to enable it it doesnt work.
and for the Main Camera i attached a “Gun” child , when i tried to enable it’s components it didnt work as well…

PLEASE HELP!

    void SpawnMyPlayer(){

        if (PT1.Length >= PT2.Length) {
           
            Debug.LogError ("Join Team 2");

            GameObject SpawnSpot2 = ST2 [Random.Range (0, ST2.Length)];
            GameObject Player2 = (GameObject)PhotonNetwork.Instantiate ("PlayerSphere2", SpawnSpot2.transform.position, SpawnSpot2.transform.rotation, 0);
            standbyCamera.SetActive (false);
            Player2.GetComponent<CharacterController>().enabled = true;
            Player2.GetComponent<MouseLook>().enabled = true;
            Player2.GetComponent<FPSInputController>().enabled = true;
            Player2.GetComponent<PManager>().enabled = true;
            ((MonoBehaviour)Player2.GetComponent ("CharacterMotor")).enabled = true;
            ((MonoBehaviour)Player2.GetComponent ("MovingMotor")).enabled = true;
            ((MonoBehaviour)Player2.GetComponent ("MovementScript")).enabled = true;

            Player2.GetComponentInChildren<Camera>().enabled = true;  //The Problem is here!! it doesnt work

Thanks for help:(

Is the Main Camera gameobject disabled when you try to get the camera component?

no mann:(

https://unity3d.com/learn/tutorials/modules/beginner/scripting/enabling-disabling-components

Perhaps you need to get the components instantiated first like in this example?

using UnityEngine;
using System.Collections;

public class EnableComponents : MonoBehaviour
{
private Light myLight;

void Start ()
{
myLight = GetComponent();
}

void Update ()
{
if(Input.GetKeyUp(KeyCode.Space))
{
myLight.enabled = !myLight.enabled;
}
}
}

Does the player actually have a child with a Camera component?
Gotta rule out the simple things first. :slight_smile:

Also, give this recorded live training a watch:
https://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/fragmas-2-multiplayer-fps

It talks about setting up simple networked players.

Yes im sure , and i’ll check this out

i’m not sure i got that… instantiating the components (if possible) would be awesome and will work i guess , but i didnt get what does it have to do with the script you added :confused: it’s basically what i did , but i got what you mean … i’ll try that out

If you plan on using the components again, then store them in variables.
Constantly using GetComponent is slower and more intensive than getting a reference to the Component.

I just copied in the example from the link i also wrote.

Doh09 and TonanBora thanks a lot both of you , i tried your ways(Adding a component) but didnt work , but what i did at last , i deleted the main camera after deattaching the gun from it , and added a Camera Component to the player , and when i instantiate the player i simply enable the camera component and the gun’s components:) … yeah as simple as that :stuck_out_tongue: i feel stupid:smile:

Yes, but now you have another problem. :stuck_out_tongue:
Now, your camera will not rotate unless the player rotates, which means no looking up, or down, unless your player physically rotates up, or down.

Don’t worry it’s easy to fix :stuck_out_tongue: , i just changed the “Mouse Look” Axes to x and y and modified it so it works exactly like the camera :slight_smile: