CharacterMovement::CmdFight() called on NetworkCharacterContainer without authority error - c#

I have been creating a multiplayer game using Mirror. I have coded an entire Fight function, and I turned it to a command function since i want it to be run on a server (because I think it's easier to calculate who you shot on server then on a client), but I get an error:
Command Function System.Void CharacterMovement::CmdFight() called on NetworkCharacterContainer(Clone) without authority
(The error on the title appears on host and I don't know why).
Here is the code:
public void HandleFighting()
{
if(Input.GetMouseButtonDown(0))
{
Debug.Log(this);
CmdFight(); //! We want to fight!
}
}
[Command]
private void CmdFight()
{
if (!isLocalPlayer)
{
Debug.LogWarning("Command Function CmdFight() called on CharacterMovement without authority.");
return;
}
//! If we clicked on left mouse button
if (WeaponData != null)
{
RaycastHit raycastHit;
if (Physics.Raycast(CharacterPosition, Input.mousePosition, out raycastHit, WeaponData.Range))
{
//!We hit a player
GameObject ho = raycastHit.collider.gameObject;
Health health;
if (ho.TryGetComponent<Health>(out health))
{
//! We hit a player
health.SubstactHealth(WeaponData.damage);
health.AddEffects(WeaponData.effects);
}
}
}
}
I am spawning player normally and on network server (like this):
public override void OnServerAddPlayer(NetworkConnectionToClient conn)
{
//TODO Read the code and fix it
GameObject Player = Instantiate(playerPrefab,transform.position,transform.rotation); //! create a player
NetworkServer.Spawn(Player);
If anybody can help, please write it down below.
I have asked ChatGP and it told me like 7 steps, i did them all and nothing worked.
Some of these steps were:
Make sure that the object that is calling the CmdFight() function has a NetworkIdentity component. You can add it by clicking on the object in the Unity editor, and then in the "Add Component" menu, searching for "Network Identity".
or
Make sure that the NetworkIdentity component is correctly set up. In the inspector, check that the "Local Player Authority" option is enabled. This option allows the object to execute command functions as a local player object.
or even
Make sure that the object was instantiated using the NetworkServer.Spawn() method or a NetworkIdentity component with the Local Player Authority option selected.

Related

How do you stop a client/host in Unity Netcode for Gameobjects?

I managed to get some kind of multiplayer working with Unity Netcode which I understand is very new, but I'm running into a pretty big issue. I can't seem to be able to disconnect from the server instance. This causes an issue in which I load the game scene and the NetworkManager instance is spawned. Then I return to the main menu scene and load the game scene again, and now there are 2 NetworkManager instances which obviously causes errors.
I read in the docs that you can use this:
public void Disconnect()
{
if (IsHost)
{
NetworkManager.Singleton.StopHost();
}
else if (IsClient)
{
NetworkManager.Singleton.StopClient();
}
else if (IsServer)
{
NetworkManager.Singleton.StopServer();
}
UnityEngine.SceneManagement.SceneManager.LoadScene("MainMenu");
}
However all these stop functions don't seem to exist at all under the singleton. Am I missing a library or something?
For now I am using a workaround which is just disabling "Don't destroy" and then the NetworkManager is destroyed after switching scenes, but what if I wanted to keep it after switching a scene? Is there no way to control when the Server/Client is stopped? I'm assuming this is a bug but I just want to make sure.
void Disconnect()
{
NetworkManager.Singleton.Shutdown();
}
Disconnects clients if connected and stops server if running.
To answer your full question, you also need a Cleanup() function in your non-networked scene that checks for astray NetworkManager singletons and destroys them. This will avoid unnecessary duplication of network managers.
void Cleanup()
{
if (NetworkManager.Singleton != null)
{
Destroy(NetworkManager.Singleton.gameObject);
}
}

I have disabled multi touch but still not working unity

I have disabled multitouch but still, when I place second finger on the screen my joystick snaps to the second finger position.
public class GameManager : MonoBehaviour
{
private void Awake()
{
Input.multiTouchEnabled = false;
}
}
I am using variable joystick from free joystick pack.
public class GameManager : MonoBehaviour
{
private void Awake()
{
Input.multiTouchEnabled = false;
}
}
That would work only if the app was installed on the device, it will not work on the editor and not even using Unity remote, since Unity remote streams your game only and passes some parameters while streaming.
For more info about Unity Remote: Unity Manual - Unity Remote

Unity Script is not running

i am creating all new unity project and added Empty GameObject in to my first Scene, and also created and added script to that object, but when i'm trying to run my scene script dose not showing any output in my console window.
here is my code
using System.Collections;
using UnityEngine;
public class ScriptObject : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("Start method called.");
}
// Update is called once per frame
void Update()
{
Debug.Log("Update method calling.");
}
}
You need to turn on showing of another types of log messages ("Info" messages in your case).
You need to be sure that your button of the console output is enabled. Just try to click on the button.Look at the screenshot

Passing a boolean between different objects not working

I know this has been covered in the past, but I believe this issue is being caused by the update to Unity 5, thus deprecating the older information. No errors come up, but the debug won't come up. The public variable of 'pushBlockEnter' does become true visibly on the player script, but the pushBlock class will not recognize it no matter what. I just want to be able to check if a variable is true in one script from another script in Unity 5, any way of doing it would be fine. I'm sure it is something simple but I just can't figure it out...I should also mention that both scripts are on a different object. Thanks in advance!:
public class pushBlock : MonoBehaviour {
public Player playerScript;
void Update () {
if (GameObject.Find("Player").GetComponent<Player>().pushBlockEnter)
{
Debug.Log ("do something blahh");
//Do Anything
}
}
}
Figured it out:
GameObject playerReference = GameObject.Find("Player");
void Update(){
if (GameObject.Find("pushBlockCollider").GetComponent<pushBlockCollider>().inPushBlock){
Debug.Log ("got to pushblockCollider True");
}

Pass Object to Lua Script from C#

I am using LuaInterface with C#, and have got everything set up correctly.
What I want to be able to do is that when the script is started using lua.DoFile(), that the script has access to a Player object that I can send...
Current Code:
public static void RunQuest(string LuaScriptPath, QPlayer Player)
{
QMain.lua.DoFile(LuaScriptPath);
}
But as you can see the script will not have access to the Player object.
I see two options. The first one is to make your player a global variable for Lua:
QMain.lua['player'] = Player
Then you'll be able to access player in your script
The second option is to have the script define a function accepting player as parameter. So if your current script contains ...code... now it will contain:
function RunQuest(player)
...code...
end
and your C# code will look something like this:
public static void RunQuest(string LuaScriptPath, QPlayer Player)
{
QMain.lua.DoFile(LuaScriptPath); // this will not actually run anything, just define a function
QMain.lua.GetFunction('RunQuest').Call(player);
}

Categories

Resources