- Home /
NullReferenceException with UNET void
So I started using Unity Networking yesterday, first following the new merry fragmas tutorial and implementing some of that in my own project. After that I wanted to sort of "sync" muzzle flashes, bullets etc. (I spawn a bullet which uses "real" ballistics and uses raycasts to check for hits)
Everything works fine, but when I shoot anywhere with the connected player, it gives this long error:
NullReferenceException: Object reference not set to an instance of an object
NetworkedPlayerScript.RpcFire (UnityEngine.GameObject projectile, UnityEngine.GameObject barrel, UnityEngine.AudioClip fireSound, UnityEngine.GameObject[] firingParticles, UnityEngine.GameObject[] deactivateObjectsOnFire, Single reactivateAfter) (at Assets/Resources/Scripts/NetworkedPlayerScript.cs:85)
NetworkedPlayerScript.InvokeRpcRpcFire (UnityEngine.Networking.NetworkBehaviour obj, UnityEngine.Networking.NetworkReader reader)
UnityEngine.Networking.NetworkIdentity.HandleRPC (Int32 cmdHash, UnityEngine.Networking.NetworkReader reader) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:652)
UnityEngine.Networking.ClientScene.OnRPCMessage (UnityEngine.Networking.NetworkMessage netMsg) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/ClientScene.cs:722)
UnityEngine.Networking.NetworkConnection.InvokeHandler (UnityEngine.Networking.NetworkMessage netMsg) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:208)
UnityEngine.Networking.LocalClient.ProcessInternalMessages () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/LocalClient.cs:141)
UnityEngine.Networking.LocalClient.Update () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/LocalClient.cs:69)
UnityEngine.Networking.NetworkClient.UpdateClients () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkClient.cs:861)
UnityEngine.Networking.NetworkIdentity.UNetStaticUpdate () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:1044)
After struggling with this all day I thought i'd post it here, maybe some of you guys know where I'm being a dumbass.
Here's the part where I get the error:
[Command]
public void CmdSendFireNetwork(GameObject projectile, GameObject barrel, AudioClip fireSound, GameObject[] firingParticles, GameObject[] deactivateObjectsOnFire, float reactivateAfter)
{
if(!isLocalPlayer)
RpcFire(projectile, barrel, fireSound, firingParticles, deactivateObjectsOnFire, reactivateAfter);
}
[ClientRpc]
public void RpcFire(GameObject projectile, GameObject barrel, AudioClip fireSound, GameObject[] firingParticles, GameObject[] deactivateObjectsOnFire, float reactivateAfter)
{
Instantiate(projectile, barrel.transform.position, barrel.transform.rotation);
barrel.GetComponent<AudioSource>().PlayOneShot(fireSound);
foreach (GameObject ps in firingParticles)
{
ps.GetComponent<ParticleSystem>().Play();
}
MuzzleFlash(deactivateObjectsOnFire, reactivateAfter);
}
private IEnumerator MuzzleFlash (GameObject[] deactivateObjectsOnFire, float reactivateAfter)
{
foreach (GameObject go in deactivateObjectsOnFire)
{
go.SetActive(true);
}
yield return new WaitForSeconds(reactivateAfter);
foreach (GameObject go in deactivateObjectsOnFire)
{
go.SetActive(false);
}
}
[Command]
public void CmdBulletHitPlayer(GameObject hit)
{
hit.GetComponent<NetworkedPlayerScript>().RpcResolveHit();
}
(this isn't the full script of course) This part of code is attached to the player which contains all the network components needed. The bullet that is supposed to instantiate is only a dummy projectile just so other players can see the bullet, the real bullet checking if it hit a player is only local.
I know I am probably doing a lot of this wrong but forgive me because it's my first time working with Networking.
Basically what happens is this; Gun fires, it tells the player object all the parameters (CmdSendFireNetwork), CmdSendFireNetwork checks if bullet was shot by the local player, if it isnt it sends all the parameters to the network only to be received again in RpcFire(), problem is, RpcFire says the error: "NullReferenceException: Object reference not set to an instance of an object".
I'm totally clueless, I also printed all the parameters in CmdSendFireNetwork just to make sure it got everything it needed, then I also printed all the parameters in RpcFire but as soon as it is called the error appears.
Please help me, and please ask for more info if you need it to answer my question.
Thanks community :)
Your answer
Follow this Question
Related Questions
Null Reference in Line 0 1 Answer
Null Reference Exception, what is it and why do I get it? 1 Answer
NullReferenceException: Object reference not set to an instance of an object 2 Answers
Why am i getting a NullReferenceException when i switch platform to Android? 0 Answers
Networking-multplayer issue 1 Answer