- Home /
Command not executing for all clients
Alright so im making a simple multiplayer soccer game, to practice some unity networking. i have a trigger collider set up in each goal, and each player disables the one in their own goal when they spawn ( so they only will keep track of when they score on the opposing goal ) this works fine, however when the player scores, i want them to send a message to the server to reset the ball, for this i used a Command, however this only worked for the host, that is because my BallMaster(the one with the command) is not instantiated on clients. Is there any other way to communicate with it to reset the ball, or is there a simple fix? these are the scripts:
public class BallMaster : NetworkBehaviour {
public GameObject ball;
public bool spawned;
public GameObject ballS;
void Start()
{
}
void Update() {
if (NetworkServer.connections.Count > 1 && !spawned && NetworkServer.active)
{
spawned = true;
ballS = (GameObject)Instantiate(ball, new Vector3(0, 0.5f, 0), Quaternion.identity);
ballS.GetComponent<Rigidbody>().velocity = Vector3.zero;
NetworkServer.Spawn(ballS);
} else if (NetworkServer.connections.Count <= 1)
{
spawned = false;
}
}
[Command]
public void CmdResetBall()
{
Debug.Log("Resetting ball");
NetworkServer.Destroy(ballS);
spawned = false;
}
}
and the goal:
public class Goal : MonoBehaviour {
public int points;
void OnTriggerEnter (Collider col)
{
if(col.tag == "Ball")
{
OnGoal();
}
}
void OnGoal()
{
points++;
GameObject.FindGameObjectWithTag("BallMaster").GetComponent<BallMaster>().CmdResetBall();
}
}
Your answer
Follow this Question
Related Questions
Why will the raycast not work on the client? 2 Answers
[Command] not sending to server? 1 Answer
Why is the command function not being called on the client? 0 Answers
Object not updating on host 1 Answer
"Trying to send command for object without authority" warning but the object is the localPlayer 1 Answer