- Home /
Im getting a "NullReferenceException: Object reference not set to an instance of an object" error not sure why.
I'm following quill18creates's tutorial on making a Multiplayer FPS and I'm on part 5 of his tutorial.
I get this error:
NullReferenceException: Object reference not set to an instance of an object NetworkManager.SpawnMyPlayer () (at Assets/NetworkManager.cs:45) NetworkManager.OnJoinedRoom () (at Assets/NetworkManager.cs:36) UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions) NetworkingPeer:SendMonoMessage(PhotonNetworkingMessage, Object[]) (at Assets/_Imported/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1955) NetworkingPeer:OnEvent(EventData) (at Assets/_Imported/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1791) 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:83)
And this is my code:
using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour {
public Camera standbyCamera;
SpawnSpot[] spawnSpots;
// Use this for initialization
void Start () {
spawnSpots = GameObject.FindObjectsOfType<SpawnSpot> ();
Connect ();
}
void Connect () {
PhotonNetwork.ConnectUsingSettings ("v1.0.0");
}
void OnGUI() {
GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString() );
}
void OnJoinedLobby() {
Debug.Log ("Joined Lobby");
PhotonNetwork.JoinRandomRoom ();
}
void OnPhotonRandomJoinFailed() {
Debug.Log ("Failed To Join Room");
PhotonNetwork.CreateRoom (null);
}
void OnJoinedRoom() {
Debug.Log ("Joined Room");
SpawnMyPlayer ();
}
void SpawnMyPlayer() {
SpawnSpot mySpawnSpot = spawnSpots [Random.Range (0, spawnSpots.Length)];
GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate ("PlayerController", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
standbyCamera.enabled = false;
((MonoBehaviour)myPlayerGO.GetComponent("FPSInputController")).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent("MouseLook")).enabled = true;
myPlayerGO.transform.FindChild("Main Camera").gameObject.SetActive(true);
}
}
Not sure where im going wrong, it's my first time using C#.
Hey yeah i'm getting the same error can't figure it out been trying for months, started happening when unity 5 came out and I downloaded it.
it means exactly what it says, your reference to the object is not set
Answer by Torigas · Jul 07, 2015 at 07:05 PM
The usual procedure in this case is to check the line where that exception happens (just double click the exception to get there) and then determining which part is not set. Usually it happens when you access a component with "yourObject.GetComponent()" an that component is not on yourObject. If the line numbering is correct, I would assume there is no MouseLook Script attached to your "myPlayerGO".