Search Unity

Trying to setup a multiplayer camera. c#

Discussion in 'Multiplayer' started by Divinitize, Nov 17, 2014.

  1. Divinitize

    Divinitize

    Joined:
    Oct 27, 2013
    Posts:
    11
    Code (csharp):
    1.  
    2.     public CameraManager CameraManager;
    3.  
    4.     void Awake()
    5.     {
    6.         CameraManager = GetComponent<CameraManager>();
    7.         if(networkView.isMine)
    8.         CameraManager.target = transform;
    9.     }
    10.  
    This is basically what i have, this is being called inside my characters movement script, CameraManager is my camera script and inside that script i have a variable called target.

    I am getting a null reference exception whenever my character is spawned.

    Can anyone see any problem or work out where i am going wrong, thanks in advance.

    I am new to c#.
     
  2. Glader

    Glader

    Joined:
    Aug 19, 2013
    Posts:
    456
    Are you sure you have a CamerManager monobehaviour attached to this gameobject? If you do confirm that it also has a networkview.
     
  3. Divinitize

    Divinitize

    Joined:
    Oct 27, 2013
    Posts:
    11


    This is my camera, like i mentioned before i am fairly new to to both unity and c# so sorry if it's anything obvious.
     
  4. Glader

    Glader

    Joined:
    Aug 19, 2013
    Posts:
    456
    It appears you're trying to get a component off of your camera but the script you're calling GetComponent<T> from doesn't actually have the behavior on it. CamerManager is on your Camera gameobject while, I'm assuming, your player's movement behavior is on your player.

    You may be best off using the Camera.main to get the gameobject of the camera and then call GetComponent<T> on that or use the various other versions of GetComponent depending on your object heriarchy, such as ComponentInParent or ComponentInChildren or etc.

    At the moment it just appears that the actually behavior, CameraManager, is not on the gameobject that the movement behavior is on.
     
  5. Divinitize

    Divinitize

    Joined:
    Oct 27, 2013
    Posts:
    11
    I really appreciate the replies, however i am still struggling, i think i am on the right path now though,

    Code (csharp):
    1.  
    2.     void Awake()
    3.     {
    4.         if (networkView.isMine)
    5.             Camera.main.GetComponent("CameraManager").target = transform;
    6.     }
    7.  
    but now it's saying UnityEngine.component does not contain the definition target.

    Any help would be highly appreciated.
     
  6. Glader

    Glader

    Joined:
    Aug 19, 2013
    Posts:
    456
    That's because you're using firstly the non-generic version so the return type is just Component. Either use GetComponent<T> or check for null and then try down casting to CameraManager.

    My recommendation is to stick with the generic GetComponent<T>.
     
  7. Divinitize

    Divinitize

    Joined:
    Oct 27, 2013
    Posts:
    11
    Code (csharp):
    1.  
    2.         if (networkView.isMine)
    3.             Camera.main.GetComponent<CameraManager>().target = transform;
    4.  
    This is what i got working in the end, turns out i was missing the parentheses () at the end.
    Hopefully this will become useful to anyone else in the future, thanks for all your support Glader.