- Home /
NullReferenceException: Object reference not set to an instance of an object NetworkManager.SpawnMyPlayer () (at Assets/NetworkManager.cs:46)
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( "MultiPlayer v001" );
}
void OnGUI() {
GUILayout.Label( PhotonNetwork.connectionStateDetailed.ToString() );
}
void OnJoinedLobby(){
PhotonNetwork.JoinRandomRoom();
}
void OnPhotonRandomJoinFailed(){
PhotonNetwork.CreateRoom( null );
}
void OnJoinedRoom(){
SpawnMyPlayer();
}
void SpawnMyPlayer(){
if(spawnSpots == null){
Debug.LogError ("QUE MIERDA!!!");
return;
}
SpawnSpot mySpawnSpot = spawnSpots[ Random.Range (0, spawnSpots.Length)];
GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("Player Controller", 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);
}
}
error: NullReferenceException: Object reference not set to an instance of an object NetworkManager.SpawnMyPlayer () (at Assets/NetworkManager.cs:46) NetworkManager.OnJoinedRoom () (at Assets/NetworkManager.cs:32) UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions) NetworkingPeer:SendMonoMessage(PhotonNetworkingMessage, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1865) NetworkingPeer:OnEvent(EventData) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1708) ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[]) ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands() ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands() PhotonHandler:Update() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:83)
Answer by Landern · Aug 20, 2014 at 07:03 PM
Do yourself a favor and debug your code. It appears it can't find the MouseLook script, adjust your code to output to your console.
SpawnSpot mySpawnSpot = spawnSpots[ Random.Range (0, spawnSpots.Length)];
GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("Player Controller", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
standbyCamera.enabled = false;
MonoBehaviour fpsInput = myPlayerGO.GetComponent("FPSInputController") as MonoBehaviour;
if (fpsInput != null)
{
fpsInput.enabled = true;
Debug.Log("FPSInputController is enabled");
}
else
{
Debug.Log("Couldn't find a reference to FPSInputController on myPlayerGO");
}
//((MonoBehaviour)myPlayerGO.GetComponent("FPSInputController")).enabled = true;
MonoBehaviour mouseLook = myPlayerGO.GetComponent("MouseLook") as MonoBehaviour;
if(mouseLook != null)
{
mouseLook.enabled = true;
Debug.Log("MouseLook is enabled");
}
else
{
Debug.Log("Couldn't find a reference to MouseLook on myPlayerGO");
}
//((MonoBehaviour)myPlayerGO.GetComponent("MouseLook")).enabled = true;
myPlayerGO.transform.FindChild("Main Camera").gameObject.SetActive(true);
now unity gives me this error
Assets/Network$$anonymous$$anager.cs(73,1): error CS8025: Parsing error
why whats meens??
thanks for your answer but now unity give me this error
Assets/Network$$anonymous$$anager.cs(66,92): error CS8025: Parsing error
whats meens??
It means the line 66 has a parsing error. Since we don't know what looks likes this line, we can't help you.
Answer by Andres-Fernandez · Aug 21, 2014 at 06:35 AM
It means you have some error in the NetworkManager script, before line 66. Check for missing parenthesis, brackets, semicolon, things like that.
This is a strange thing, as
(($$anonymous$$onoBehaviour)myPlayerGO.GetComponent("$$anonymous$$ouseLook")).enabled = true;
used to work. In fact it still does on one of my other projects. On another project, it does not. The code in the answer above with debug logs does fix it, but in a strange way! The log still reports that it can't find a reference to $$anonymous$$ouseLook, but $$anonymous$$ouseLook is being enabled. Using the code without the debug stuff....
$$anonymous$$onoBehaviour mouseLook = myPlayerGO.GetComponent("$$anonymous$$ouseLook") as $$anonymous$$onoBehaviour;
mouseLook.enabled = true;
makes it not work again! Anyone care to explain this? I'd like to know the logic here. Cheers
Well, it's been so long since this code was posted and we don't know what version of Unity you are using... But, one thing comes to my $$anonymous$$d: You are creating an instance of what I believe is a prefab of your player, right? Are you sure that the prefab doesn't come with the $$anonymous$$ouseLook component already enabled? Because that might be the case: new instance has the $$anonymous$$ouseLook component enabled, but GetComponent can't find the component itself.
Also, have yo tried getting the component as $$anonymous$$ouseLook ins$$anonymous$$d of $$anonymous$$onoBehaviour?
$$anonymous$$ouseLook mouseLook = myPlayerGO.GetComponent<$$anonymous$$ouseLook>();
if(mouseLook != null) {
mouseLook.enabled = true;
}
Thanks for the response! I've already fixed the issue with the code in the answer, even though as I said, it still reports not finding the $$anonymous$$ouseLook component. The network manager code originally came from a youtube tutorial and searching the problem gave many posts about the same code. I just don't understand why the same code works in my other project, but not this current one and countless other peoples projects. Also, why does the code in the answer above work and still report as though it doesn't? Both of my projects are using Unity 5
Your answer
![](https://koobas.hobune.stream/wayback/20220613161055im_/https://answers.unity.com/themes/thub/images/avi.jpg)