- Home /
[Command] call is not being executed
I'm currently making a test program that moves a player upward after a button is clicked. I'm using commands and RPC's to get this done. When I click the button on the host, the host player moves upwards, but not when I click the button on the client. I cannot figure out why. I'm getting an error saying that I'm "Trying to send command for object without authority". But the player should already have authority right?! The player prefab has a network identity with Local Player authority checked. What's weird is that my code that changes the names of each player when they spawn in works perfectly, but not the code that changes the position for the client at least.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class TestPlayerController : NetworkBehaviour {
void Start() {
if (!isServer)
{
CmdChangeName("Player 2");
}
else if (isServer) {
CmdChangeName("Player 1");
}
}
void Update() {
if (TestCubeScript.button1Press == true) {
if (!isServer) {
CmdMoveCube(new Vector2(transform.position.x, transform.position.y + 1));
TestCubeScript.button1Press = false;
}
else {
RpcUpdateCube(new Vector2(transform.position.x, transform.position.y + 1));
TestCubeScript.button1Press = false;
}
}
}
[Command]
void CmdMoveCube(Vector2 pos) {
RpcUpdateCube(pos);
}
[ClientRpc]
void RpcUpdateCube(Vector2 pos)
{
transform.position = pos;
}
[Command]
void CmdChangeName(string name) {
RpcUpdateName(name);
}
[ClientRpc]
void RpcUpdateName(string name) {
transform.name = name;
}
}
Your answer
Follow this Question
Related Questions
How to write a multiplayer game that uses a dedicated server? 0 Answers
Unity networking tutorial? 6 Answers
How should I approach multiplayer? 1 Answer
Unity relay server without matchmaking 0 Answers