Quantcast
Channel: Questions in topic: "twice"
Viewing all articles
Browse latest Browse all 70

Function called twice, problem, callback

$
0
0
So I'm trying to make Multiplayer game for the first time. My game is like a Star Wars Space Fight using x wings, they shoot 4 blast or lasers when I press "space", each laser takes off 1 point of the health of the other player. Until now everything is good except for this part. When the laser touches the other player, the function "CmdJoueurTouche" is called twice (I putt a print on it). Thanks to the print I saw this. ---------- First time it goes into the script: UnityEngine.MonoBehaviour:print(Object) ScriptCanonsVaisseau:CmdJoueurTouche(String, Int32) (at Assets/Scripts/ScriptCanonsVaisseau.cs:69) ScriptCanonsVaisseau:CallCmdJoueurTouche(String, Int32) ScriptCanonsVaisseau:OnCollisionEnter(Collision) (at Assets/Scripts/ScriptCanonsVaisseau.cs:60) ---------- Second time: UnityEngine.MonoBehaviour:print(Object) ScriptCanonsVaisseau:CmdJoueurTouche(String, Int32) (at Assets/Scripts/ScriptCanonsVaisseau.cs:69) ScriptCanonsVaisseau:InvokeCmdCmdJoueurTouche(NetworkBehaviour, NetworkReader) UnityEngine.Networking.NetworkCallbacks:LateUpdate() (at Library/PackageCache/com.unity.multiplayer-hlapi@1.0.4/Runtime/NetworkCallbacks.cs:11) ---------- I saw that the second time it was a called in a Callback, is there a way to stop the callback from executing my function again? Or any other solution to my problem? pleas :) my script is below. ---------- // Script attached to my prefab player // I'm using UnityEngine.Networking; // Sorry, my names for gameObjects and Functions are in french ---------- public int canonsDommage = 1; // this is the amout of damage that the lasers do ---------- // This makes an instance of my laser and spawn it in the server for the other players [Command] void CmdTirer(Vector3 laserPosition, Quaternion laserRotation) { GameObject Laser = Instantiate(laserPrefab, laserPosition, laserRotation); Laser.GetComponent().velocity = Laser.transform.up * 50; NetworkServer.Spawn(Laser); Destroy(Laser, 2); } void OnCollisionEnter(Collision infoCol) { // If the player is touched by the "laser"... if (infoCol.collider.tag == "Laser") { // This function is called... CmdJoueurTouche(ScriptControleVaisseau.colliderVaisseau.name, canonsDommage); } } [Command] // This function searches the player in a dicionary so it knows to which player apply damage public void **CmdJoueurTouche**(string ID, int dommage) { **print**(gameObject.name); GestionnaireJoueurs joueur = ScriptGestionnaireJeu.PrendreJoueur(ID); // This searches the Player using the ID in a Dictionnary in "GestionnaireJoueurs" --> it's a PlayerManager **print**(joueur + " has been shot"); joueur.EnleverVie(dommage); // This makes damage to my player, this function is in another script, -1 }

Viewing all articles
Browse latest Browse all 70

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>