- Home /
Stupid "Reference Exception Error"
Hello! I am getting quite annoyed with this error. I added something to my multiplayer FPS game and have not touched this piece of code that had been working for quite a while, then I started getting this error every time I started to spawn my character. It is really annoying, here is the code.
public void SpawnAssaultSoldier(int teamID) { this.teamID = teamID; hasPickedTeam = true; AddChatMessage(PhotonNetwork.player.name + " Joined the Game");
if(spawnSpots == null) {
Debug.LogError ("Say What!");
return;
}
SpawnSpot mySpawnSpot = spawnSpots[ Random.Range (0, spawnSpots.Length) ];
GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("PlayerController.Assault", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
((MonoBehaviour)myPlayerGO.GetComponent("MouseLook")).enabled = true;
Debug.Log ("Mouse Look Enabled for " + PhotonNetwork.player.name);
((MonoBehaviour)myPlayerGO.GetComponent("PlayerMovement")).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent("PlayerShooting")).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent("MouseLock")).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent("ScopeControl")).enabled = true;
myPlayerGO.GetComponent<PhotonView>().RPC ("SetTeamID", PhotonTargets.AllBuffered, teamID);
standbyCamera.SetActive(false);
myPlayerGO.transform.FindChild("Main Camera").gameObject.SetActive(true);
//myPlayerGO.transform.FindChild("MiniMapCamera").gameObject.SetActive(true);
}
The error message :
NullReferenceException: Object reference not set to an instance of an object NetworkManager.SpawnAssaultSoldier (Int32 teamID) (at Assets/NetworkManager.cs:630) NetworkManager.OnGUI () (at Assets/NetworkManager.cs:396)
I'm trying to release this version of my game within 48 hours, so please try to help me quickly. Also note that I did not paste all my code into this box, just the stuff where the error was, thank you all.
Does your SpawnAssaultSolider game object need to be hooked up as a property of another script? $$anonymous$$aybe that connection broke and your network manager does not know what to use as you SpawnAssaultSolider.
Sorry if that wasn't helpful, it's not completely clear what the problem is.
Answer by spiceboy9994 · Feb 26, 2015 at 08:09 PM
Since you copied just the piece of the code that might have the issue, not easy to tell about the real problem. But I can do a hard guess:
Object reference not set to an instance of an object NetworkManager.SpawnAssaultSoldier
This means that the code tried to find the method SpawnAssaultSoldier within the object of type NetworkManager. I suppose it is a class name right?. Did you tried to call that method within the same NetworkManager class? If that's the case the call will be simply SpawnAssaultSoldier(id)
or this.SpawnAssaultSoldier(id)
. If you tried to call the method from another component, since your method is not STATIC you should call something like:
gameObject.GetComponent<NetworkManager>().SpawnAssaultSoldier(id);
where gameObject is the Game Object that has the NetworkManager monobehavior attached to it.
If you want to call the method by doing NetworkManager.SpawnAssaultSoldier(id), your method MUST be declared as static, but it may require other changes on your code (since static methods cannot call not static methods).
Hope this points you on what the real problem is.
The network manager is attached to an empty Gameobject within the scene. There is a Prefab called " PlayerController.Assault " which, you are correct, is a class type. The only error there is with the $$anonymous$$ouse Look, that is the line of code which is triggering an error. When I delete that line of code, the player can spawn again, which works, but is not perfect. I need the $$anonymous$$ouseLook to be enabled on spawn. I have a $$anonymous$$ouse Look component disabled on the prefab, which when the player spawns it is enabled. I have had no problem with this until now. (Of course is when my release date is -_-)
Hopefully this gives you some more information.
So just to be clear, are you trying to call that Network$$anonymous$$anager.SpawnAssaultSoldier from wihin your PlayerController.Asault class... if this is the case the gameObject.GetComponent().SpawnAssaultSoldier(id);
is the approach to follow. Just be sure that the gameObject points to your empty Gameobject that has the Network$$anonymous$$anager script attached.
Regards
Sort of, one thing is that also within the Network $$anonymous$$anager, there is a GUI button that when you click it is triggers the void, and the void locates PlayerController.Assault (with photon, everything that is instantiated is located in a folder in the assets) the prefab is then instantiated, with the modifications that the void provides.
How about pasting the line where you are calling that method? and the class definition where that method call is placed?