Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Not entering Cmd code on client

Discussion in 'Multiplayer' started by no_escher, Sep 17, 2017.

  1. no_escher

    no_escher

    Joined:
    Sep 17, 2017
    Posts:
    3
    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 :/
    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.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6.  
    7. public class AttackChangeCol : NetworkBehaviour
    8. {
    9.  
    10.     RaycastHit hit;
    11.     public float hitDistance = 1;
    12.     Vector3 offsetPos;
    13.     GameObject hitterPrefab;
    14.  
    15.     // Use this for initialization
    16.     void Start()
    17.     {
    18.         offsetPos = new Vector3(0, .5f, 0);
    19.         hitterPrefab = (GameObject) Resources.Load ("AttackChangeColHitter");
    20.     }
    21.  
    22.     [Command]
    23.     void CmdSpawnHitter(){
    24.         print ("instantiating hitter");
    25.         GameObject hitter = (GameObject) Instantiate (hitterPrefab, hit.transform.position, Quaternion.identity);
    26.         NetworkServer.Spawn (hitter);
    27.         print ("hitter spawned");
    28.         Destroy (hitter, 1f);
    29.     }
    30.  
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35.         Debug.DrawRay(transform.position + offsetPos, transform.forward * hitDistance, Color.green);
    36.         if (Input.GetKeyDown(KeyCode.Space) && isLocalPlayer)
    37.         {
    38.             if (Physics.Raycast(transform.position+offsetPos, transform.forward, out hit))
    39.             {
    40.                 if (Vector3.Distance(hit.transform.position, transform.position) <= hitDistance)
    41.                 {
    42.                     print ("looking");
    43.                     if (hit.transform.CompareTag("Player"))
    44.                     {
    45.                         print ("try change col");
    46.                         CmdSpawnHitter ();
    47.                         print ("done");
    48.                        
    49.                     }
    50.                 }
    51.             }
    52.         }
    53.     }
    54. }
    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.
     
  2. HotPhone

    HotPhone

    Joined:
    May 14, 2014
    Posts:
    53
    [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?
     
    no_escher likes this.
  3. no_escher

    no_escher

    Joined:
    Sep 17, 2017
    Posts:
    3
    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!