Not entering Cmd code on client

Hi;
I’m working on my first multiplayer project, and I ran into an issue:
In one of my scripts, when on the Client side, it doesn’t enter the Cmd code (to spawn an object).
When on the server, everything works fine, the object is spawned and detected from all sides.

The script is on my Player object, that has a Network Identity (local player authority checked; the spawned object’s Network Identity has everything unchecked) , a Network Transform, and another script that uses Commands and works fine :confused:
I don’t get any error messages either, but any print()s inside of the Command function are ignored, the client just skips that part.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;


public class AttackChangeCol : NetworkBehaviour
{

    RaycastHit hit;
    public float hitDistance = 1;
    Vector3 offsetPos;
    GameObject hitterPrefab;

    // Use this for initialization
    void Start()
    {
        offsetPos = new Vector3(0, .5f, 0);
        hitterPrefab = (GameObject) Resources.Load ("AttackChangeColHitter");
    }

    [Command]
    void CmdSpawnHitter(){
        print ("instantiating hitter");
        GameObject hitter = (GameObject) Instantiate (hitterPrefab, hit.transform.position, Quaternion.identity);
        NetworkServer.Spawn (hitter);
        print ("hitter spawned");
        Destroy (hitter, 1f);
    }


    // Update is called once per frame
    void Update()
    {
        Debug.DrawRay(transform.position + offsetPos, transform.forward * hitDistance, Color.green);
        if (Input.GetKeyDown(KeyCode.Space) && isLocalPlayer)
        {
            if (Physics.Raycast(transform.position+offsetPos, transform.forward, out hit))
            {
                if (Vector3.Distance(hit.transform.position, transform.position) <= hitDistance)
                {
                    print ("looking");
                    if (hit.transform.CompareTag("Player"))
                    {
                        print ("try change col");
                        CmdSpawnHitter ();
                        print ("done");
                        
                    }
                }
            }
        }
    }
}

For example, I see “looking” (line 42), “try change col” (45) and “done”(47), but not “instantiating hitter” (24) or “hitter spawned” (27).
I have watched several shooter tutorials and read documentation, but I still couldn’t figure out what the issue was.

Thanks in advance for your help.

[Command] is only called on the server, that’s why nothing gets logged on the client. Cant help you more because I never used Spawn(). Did you add the hitter into the spawnable prefabs in the NetworkManager?

1 Like

Thanks a lot! I didn’t know that before (that Command error messages are only logged on the server), so I just had to launch the server in the unity project and the client from a build (I usually go the other way around) to actually get the error messages, which were very helpful and let me find and fix the mistake I’d made (a dumb one).
I was running in circles for what felt like forever, thanks again!