Reference lost in NetworkEntity
Hello everyone, I'm a newbie in the use of the UNET and I encounter some issue I quite not understand :
My player is a Tank which can shoot "Shells". So, my GameObject "Tank" has a component script named "Player" which contains Id and nickname of the player, stuff like that.
In the "Shell" GameObject, I have a script "Shell" where I've put a property name "Player".
When I shoot the shell, I inject, just after the Instantiate(), the reference of the object the Player into the property of the same name into the shell, so I can track who shot what on the map.
This is the code :
[Command]
void CmdShoot(){
GameObject shell = (GameObject)Instantiate(gameObject.GetComponent<Tank>().Shell,Canon.transform.position, Canon.transform.rotation);
shell.GetComponent<Shell>().setPlayer(gameObject.GetComponent<Player>());
shell.SetActive(true);
shell.GetComponent<Rigidbody>().AddForce(CanonBase.transform.forward * shell.GetComponent<Shell>().Velocity); // If inactive : velocity lost after activation
NetworkServer.Spawn(shell);
}
As you can guess, this is the "Network" implementation of the code. Which works perfectly fine... as long as you are the server...
When you're a Client, and you shoot AND jump (yes you can jump) as the same time, something happen that I dont understand : Because the force of the jump can be, in some case, bigger than the ejection force of the shell, the shell will go through the canon from the bottom side sometimes (cause the shell is not a solid but a trigger), the shell will hit the chassis (base) of the tank who shot it. It can be manageable as long as I can detect the fact than the shell is from the same player (and so, ignore the collision and continue its course) but, IN SOME RANDOM CASES, the reference to the player is lost/never injected, and I get a "NullReferenceException" in this function
void OnTriggerEnter(Collider collider){
GameObject target = collider.gameObject;
Debug.Log("Shell Player : " + this.player.ToString()); // NullReferenceException
if(!target.name.Equals("Canon") && !target.name.Equals("Weight")){
if(target.GetComponent<TankPart>() != null){
Debug.Log("Target Id : " + target.GetComponent<TankPart>().Player.Id);
Debug.Log("Shell Id : " + this.player.Id);
if(!target.GetComponent<TankPart>().Player.Id.Equals(player.Id)){
target.GetComponent<TankPart>().takeDamages(gameObject);
}
}
Debug.Log(collider.ToString());
NetworkServer.Destroy(gameObject);
}
To me it's like the physic continue his calculation at the same time the rest of it do the execution of the first code, and the trigger is detected between the 1st and 2nd line of the CmdShoot() function (before the reference is sent). Also, I said it looks like it's sometime never injected, because sometime I dont jump, the shell goes, and when it hits the ground, I got a "NullReferenceException" too. Maybe it's the same case, it's just hard to tell.
The call of the CmdShoot() was at first in the Update() loop, so I've also tried to put it into the FixedUpdate() loop (after all, it's a physical action, so, it makes sens to put it here after all) but the issue came anyway (maybe more rarely. Hard to tell, again). In my latest test, the Issue is encounter 1/28 shoot. Which is a lot.
I've also my console which is full of
"Did not find target for sync message for 175 UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()"
I dont think it's related, but I think it's good to notify that to you all.
I'm pretty desperate, I'm working on this issue since last Sunday and I can't find any long term solution. I hope someone here can help me.
I hope my post is clear. English is not my first language :/.
Cheers everyone. Thank in advance.
Your answer
Follow this Question
Related Questions
unity networking start host with autocreate 0 Answers
How to count score on all players from non player object 0 Answers
How do I get the true position (Vector 3) from a Network Transform Component(Unet)? 0 Answers
Day/Night cycle turns black at night and how to Serialize it rightly? 0 Answers
Photon Networking - How to move all players from game scene to current room scene? 0 Answers