Trying to send command for object without authority. How to fix?

image
How do I fix this?!?! this is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using TMPro;

public class Player : NetworkBehaviour
{
[Header(“Player Movement Values:”)]
[SerializeField] float moveSpeed = 5;
[SerializeField] float jumpForce = 5;
[Header(“Player Information”)]
[SyncVar(hook = “handleDisplayNameUpdated”)] [SerializeField] private string displayName = “Missing Name”;
[SyncVar(hook = “handleDisplayColourUpdated”)] [SerializeField] private Color Color = new Color(1,1,1,1);
[Header(“References:”)]
[SerializeField] SpriteRenderer sr;
[SerializeField] Rigidbody2D rb;
[SerializeField] TMP_Text displayNameText;

private void Update()
{
    handleMovement();
}

#region Player Movement
void handleMovement()
{
    if (isLocalPlayer)
    {
        float mx = Input.GetAxisRaw("Horizontal");

        transform.position = new Vector3(transform.position.x + mx * Time.deltaTime * moveSpeed, transform.position.y);
        if (Input.GetKeyDown(KeyCode.W))
        {
            rb.velocity = transform.up * jumpForce;
        }
    }
}

#endregion

#region Server

[Server] // [server] not needed it just prevents client from calling it
public void setDisplayName(string newDisplayName)
{
    if (newDisplayName == "Frick") { return; }
    else
    {
        displayName = newDisplayName;
    }   
}

[Server] // [server] not needed it just prevents client from calling it
public void setVisibleColor(Color newColor)
{
    Color = newColor;
}

[Command] //Command: For CLIENTS CALLING a method ON THE SERVER
private void CmdSetDisplayName(string newDisplayName)
{

    RpcLognewName(newDisplayName);

    setDisplayName(newDisplayName);

}

#endregion

#region Client

private void handleDisplayNameUpdated(string oldName, string newName)
{
    displayNameText.text = newName;
}
private void handleDisplayColourUpdated(Color oldColor, Color newColor)
{
    sr.color = newColor;
}

[ContextMenu("Set My Name")]
public void setMyName()
{
    CmdSetDisplayName("My New Name");

}

[ClientRpc] //Ran On the Client Called by the server - Client Rpc's are for the SERVER calling a method on ALL CLIENTS
private void RpcLognewName(string newDisplayName)
{
    Debug.Log(newDisplayName);
}

#endregion

}

Hi there,
Is this happening when you try and set the name from the context menu?

Yes, It happens when I do it for the person that joined the host, It works for the host.

I also downloaded the script that the instructor wrote and it still had the issue, So I guess its a mirror update or I have done something in the unity editor however I don’t think I have as I have recreated the Network manager 3 times and all times it had the exact same issue.

I still havent figured out what ive done wrong

Hi There,
My understanding is that running the method from the context menu does not supply the proper authority to use a Cmd function. This maybe be a change in Mirror since the course was created. This is okay, this is just an early test to show the method is working. You should be able to continue on with the course, this shouldn’t affect any future code.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms