- Home /
Why is the command function not being called on the client?
I am not sure why the CmdSpawn code isn't being called. It doesn't print the dropLocation in the console but it spawns a block at 0,0,0. The code works fine on the host but not on the client. I have been struggling on this same thing for about 2 weeks and I am sick of it. Please help, here is the current code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;
public class Shooting : NetworkBehaviour {
[SerializeField] public Transform shootLocation;
[SerializeField] public GameObject cannonBall;
[SerializeField] public float shootingSpeed;
[SerializeField] public static int blocks;
[SerializeField] public GameObject cube;
[SerializeField] public static Vector3 dropLocation;
void Start () {
blocks = 5;
}
void Update () {
if (Input.GetMouseButtonDown(0))
{
if (blocks > 0)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.name != "null" && hit.transform.name != "wall")
{
dropLocation = new Vector3(hit.point.x, hit.point.y + 1.75f, hit.point.z);
CmdSpawn();
print(blocks);
}
}
}
else
{
CmdFire();
}
}
}
[Command]
void CmdSpawn()
{
print(dropLocation);
blocks = blocks - 1;
GameObject block = Instantiate(cube, dropLocation, Quaternion.identity);
NetworkServer.Spawn(block);
}
[Command]
void CmdFire()
{
var shell = Instantiate(cannonBall, shootLocation.position, shootLocation.rotation);
shell.GetComponent<Rigidbody>().velocity = shell.transform.forward * shootingSpeed;
NetworkServer.Spawn(shell);
}
}
I am still not sure why the function isn't being called. The object calling the command is the player prefab and has a network identity set to the local player authority. I need help
Is unity answers still active because I have posted about 5 questions and not a single one has been commented on or answered
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
Command not executing for all clients 0 Answers
Force local client RPC to fire immediately? 0 Answers
Networking bullet spawning issues between Host/Client 0 Answers