So i am new to Mirror and i made this to learn a bit,it changes name of the host normally but when i try it on a client he just disconnects,and without any errors.I have setup many debug.log statements to see where is the problem and it seems that is not even executing as a client,it just stops before it was called.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using TMPro;
using System;`
public class PlayerMovement : NetworkBehaviour
{
public float speed;
float x, y;
Rigidbody2D rb;
Animator animator;
public TMP_InputField tmpf;
public TMP_Text nameText;
[SyncVar(hook = nameof(DisplayName))]
public string _name = "00";
void DisplayName(string oldName, string newName)
{
nameText.text = newName;
}
void Start()
{
if (!isLocalPlayer) return;
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
tmpf = GameObject.Find("NameIF").GetComponent<TMP_InputField>();
//tmpf.onValueChanged.AddListener(delegate { CmdChangeName(); });
}
void Update()
{
if (!isLocalPlayer) return;
x = Input.GetAxis("Horizontal");
y = Input.GetAxis("Vertical");
rb.velocity = new Vector2(x * speed, y * speed);
animator.SetFloat("horizontal", x);
animator.SetFloat("vertical", y);
if (Input.GetKeyDown(KeyCode.KeypadEnter))
{
CmdChangeName();
}
}
[Command]
public void CmdChangeName()
{
_name = tmpf.text;
}
How to fix this,i have client authority checked and i need to use commands if i want hooks and syncVar to work.