- Home /
Duplicate Question
Object Reference Not Set To An Instance Of An Object!
Hey! I've been trying to get a simple UFPS player to work over a Photon Network. I came across this error:-
NullReferenceException: Object reference not set to an instance of an object NetworkManager.SpawnMyPlayer () (at Assets/NetworkManager.cs:47) NetworkManager.OnJoinedRoom () (at Assets/NetworkManager.cs:35) UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions) NetworkingPeer:SendMonoMessage(PhotonNetworkingMessage, Object[]) (at Assets/_Imported/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1769) NetworkingPeer:OnEvent(EventData) (at Assets/_Imported/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1612) 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:76)
In my script I referenced a component on my UFPS Player GO "Vp_FPController" Here is the script :-
using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour {
SpawnSpot[] spawnSpots;
// Use this for initialization
void Start () {
spawnSpots = GameObject.FindObjectsOfType<SpawnSpot>();
Connect ();
}
void Connect() {
PhotonNetwork.ConnectUsingSettings( "FPS Game 0.00004" );
}
void OnGUI() {
GUILayout.Label( PhotonNetwork.connectionStateDetailed.ToString() );
}
void OnJoinedLobby() {
Debug.Log ("OnJoinedLobby");
PhotonNetwork.JoinRandomRoom();
}
void OnPhotonRandomJoinFailed() {
Debug.Log ("OnPhotonRandomJoinFailed");
PhotonNetwork.CreateRoom( null );
}
void OnJoinedRoom() {
Debug.Log ("OnJoinedRoom");
SpawnMyPlayer();
}
void SpawnMyPlayer() {
if(spawnSpots == null) {
Debug.LogError ("NO SPAWN POINTS FOUND!!!");
return;
}
SpawnSpot mySpawnSpot = spawnSpots[ Random.Range (0, spawnSpots.Length) ];
GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate ("SimplePlayer", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
((MonoBehaviour)myPlayerGO.GetComponent("Vp_FPController")).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent("Vp_FPinput")).enabled = true;
}
}
I'm not sure why the GameObject isn't being referenced as I am in the script clearly asking to enable it (The script is supposed to enable the component once the player spawns into the game.)
I have disabled the component, could this be why? I've tryed it with the component activated but that doesn't do what I want it to do...
If you guys need to know anything else please be sure to ask and I'll provide.
I re-tagged your question so it links to the 50 pages of questions about null reference exceptions.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
NullReferenceException problem 2 Answers
NullReferenceException: I think unity bug 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers