CmdSetTarget is not being called

Hi, I’m following the Unit targeting lesson from Unity Multiplayer: Intermediate C# Coding & Networking course but I’m stuck with an error I can’t solve.
I’ve seen another post here with the same error but that topic was closed and so there is no solution there.
I’ve got the same code that is seen in the lesson, I’ve set some debug lgos inside my functions to let me see when it stops and to my surprise the method “CmdSetTarget” is not getting called.
Here’s my code:

public void CmdSetTarget(GameObject targetGameObject)
    {
        Debug.Log("in CmdSetTarget");
        try
        {
            if (!targetGameObject.TryGetComponent<Targetable>(out Targetable target))
            {
                Debug.LogError("This game object does NOT have the Targetable component");
            }
            else
            {
                Debug.Log("Target set");
                this.target = target;
                Debug.Log(this.target);
            }
        }
        catch (UnityException error)
        {
            Debug.LogError(error);
        }
    }

The code you see above is in the Targeter.cs file.
The following is inside the UnitCommandGiver.cs file.

 private void TryTarget(Targetable target)
    {
        Debug.Log("In TryTarget");
        foreach (Unit unit in unitSelectionHandler.selectedUnits)
        {
            Debug.Log("Setting the target");
            unit.GetTargeter().CmdSetTarget(target.gameObject);
        }
    }

In my case the string “In CmdSetTarget” never shows up in the console.
What could it be the error?

1 Like

So I’ve found out a workaround but I don’t think this is a good one. Removing the [Command] in the script actually made it work. Why is that?

1 Like

Hi there, are you debugging from the host or from a client in the editor?

The command attribute signifies that the call is made by a client to the server.

If it is not getting called from a client, it could be because the selectedUnits list is empty in the client.

Are you able to debug from a client and see if it’s getting called from there? At the same time you can check if it sees that the units are selected?

1 Like

Privacy & Terms