- Home /
Destroying entire parent object on one client but on the other destroying just the child.
This is going to be a lot of text but i really need help so please try to bare with me. I am currently making a multiplayer space combat building game (kinda like space engineers). I need the parts to be destroyed when they take damage. The problem is when one part takes enough damage to be destroyed, to the player who shot the other ship the entire ship is destroyed when one part dies. But to the player who has been shot, it works fine and only one part is destroyed. Another strange thing is that to the player who has been shot, the other players ship is gone too. I have a photon view observing the parts script on each part. I'm going to post the important parts of the script in order of what happens.
To start here is the bullet script.
void OnTriggerEnter(Collider othercol) {
if (othercol.gameObject) {
if (gameObject != ShooterShip) {
Debug.Log (othercol.name);
if (othercol.GetComponent<Parts> () != null) {
othercol.GetComponent<Parts> ().GetComponent<PhotonView> ().RPC ("TakeDamage", PhotonTargets.AllBufferedViaServer, damage);
//PhotonNetwork.Destroy (this.gameObject);
} else {
//PhotonNetwork.Destroy (this.gameObject);
}
}
//PhotonNetwork.Destroy (this.gameObject);
}
}
This checks if the bullet hits a gameObject with the parts script and tells that's parts script to "TakeDamage".
The next is the Parts script.
[PunRPC]
public void TakeDamage(float damage) {
PartHp -= damage;
transform.parent.GetComponent<FlightSystem> ().GetComponent<PhotonView> ().RPC ("DestroyPart", PhotonTargets.AllBufferedViaServer);
}
This subtracts the hp from the individual part and tell the main flight script to calculate if any part is destroyed. Next is the flight scripts calculations.
[PunRPC]
public void DestroyPart(){
foreach (GameObject p in PartsGO) {
if(p.GetComponent<Parts>()){
if (p.GetComponent<Parts> ().PartHp <= 0) {
if (p.GetComponent<Parts> ().CockPit == true) {
NetworkManager nm = GameObject.FindObjectOfType<NetworkManager> ();
nm.standbyCamera.SetActive (true);
PhotonNetwork.Destroy (this.gameObject);
} else {
PhotonNetwork.Destroy (p.gameObject);
}
}
}
}
}
This checks the list of parts and sees if any part has an hp of less than 1. If it does it is suppose to destroy the single part. Please help i have been trying to get this is works for days.
Hi,
there is one thing I noticed in the OnTriggerEnter callback. With the current implementation this function gets called on each client, resulting in multiple TakeDamage calls if I'm not mistaken. To avoid this you can add a PhotonView.is$$anonymous$$ine
condition at the beginning of the OnTriggerEnter function. Since I'm not sure if there are other problems or if this is already enough to solve the problem, please add the above mentioned condition and see if this already helps. In case it is still not working, please let us know.
Is there any reason why you want the clients to handle the execution of takedamage?
It would probably make more sense to handle that on the server side and make use of networked events to inform the clients that something happened, so they can then sync health values and what not.
There is no real 'server-side' when using the Photon Cloud which I assume. If you mean, that only the $$anonymous$$asterClient should handle OnTriggerEnter function, then you have another possibility to go with. In this case you can use the PhotonNetwork.is$$anonymous$$asterClient
condition ins$$anonymous$$d of photonView.is$$anonymous$$ine
.
Answer by TheGri108 · Feb 07, 2021 at 03:18 AM
Sorry for the necro, but there is no other forums that contain a legit answer.
We found that by changing the value of isRuntimeInstantiated to false (view.isRuntimeInstantiated = false;) before running the PhotonNetwork.Destroy on the view, this solves the problem, when that value is true it finds the parent instantiationId, when it is false it uses the child photonview id instead