Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

nullreferenceexception: object reference not set to an instance of an object

Discussion in 'Scripting' started by sdfteh, Sep 1, 2015.

  1. sdfteh

    sdfteh

    Joined:
    Sep 1, 2015
    Posts:
    2
    i try to enable camera when spawn player
    but in this segment dont work
    myPlayerGO.GetComponentInChildren<Camera>().enabled = true;
    and game stop(pause)
    whole error
    NullReferenceException: Object reference not set to an instance of an object
    NetworkManager.SpawnMyPlayer () (at Assets/NetworkManager.cs:53)
    NetworkManager.OnJoinedRoom () (at Assets/NetworkManager.cs:37)
    UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
    NetworkingPeer:SendMonoMessage(PhotonNetworkingMessage, Object[]) (at Assets/_Imported/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2084)
    NetworkingPeer:OnEvent(EventData) (at Assets/_Imported/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1885)
    ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[])
    ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
    ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
    PhotonHandler:Update() (at Assets/_Imported/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:92)
    all work without en-dis camera
    but i dont need 2 only 1
     
  2. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Would you please show the relevant code? Please don't forget to add code tags.
    I am assuming your myPlayerGO has a child with a camera component attached to it?
     
    Last edited: Sep 1, 2015
  3. sdfteh

    sdfteh

    Joined:
    Sep 1, 2015
    Posts:
    2
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NetworkManager : MonoBehaviour {
    5.  
    6.     public GameObject standbyCamera;
    7.     SpawnSpot[] spawnSpots;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         spawnSpots = GameObject.FindObjectsOfType<SpawnSpot>();
    12.         Connect ();
    13.     }
    14.    
    15.     void Connect () {
    16.         PhotonNetwork.ConnectUsingSettings ("Версия 0.001");
    17.     }
    18.  
    19.     void OnGUI() {
    20.         GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString ());
    21.     }
    22.  
    23.     void OnJoinedLobby() {
    24.         Debug.Log ("OnJoinedLobby");
    25.         PhotonNetwork.JoinRandomRoom ();
    26.     }
    27.  
    28.     void OnPhotonRandomJoinFailed() {
    29.         Debug.Log ("OnPhotonRandomJoinFailed");
    30.         PhotonNetwork.CreateRoom (null);
    31.     }
    32.  
    33.     void OnJoinedRoom () {
    34.         Debug.Log ("OnJoinedRoom");
    35.  
    36.         SpawnMyPlayer();
    37.     }
    38.  
    39.     void SpawnMyPlayer (){
    40.         if(spawnSpots == null){
    41.             Debug.LogError("no spawnspots");
    42.                 return;
    43.         }
    44.  
    45.         SpawnSpot mySpawnSpot = spawnSpots [Random.Range (0, spawnSpots.Length)];
    46.         GameObject myPlayerGO =(GameObject)PhotonNetwork.Instantiate ("PlayerController", mySpawnSpot.transform.position,mySpawnSpot.transform.rotation, 0);                          
    47.         standbyCamera.SetActive(false);
    48.  
    49.         ((MonoBehaviour)myPlayerGO.GetComponent("FPSInputController")).enabled = true;
    50.         ((MonoBehaviour)myPlayerGO.GetComponent("MouseLook")).enabled = true;
    51.         ((MonoBehaviour)myPlayerGO.GetComponent("CharacterMotor")).enabled = true;
    52.         myPlayerGO.GetComponentInChildren<Camera>().enabled = true;
    53.         myPlayerGO.GetComponentInChildren<AudioListener>().enabled = true;
    54.         myPlayerGO.GetComponent<CharacterController>().enabled = true;
    55.  
    56.  
    57.     }
    58. }    
    yes standart old(UNITY 4.5 i think) Firs person controller