Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by RamonLion · Mar 01, 2017 at 03:50 AM · networkingcommandchat

Player Object does not have authority?

I'm just beginning practice with network programming and multiplayer.

I've set up a very basic chat script that I've put onto my Player Object prefab.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Chat : NetworkBehaviour
 {
     private InputField TextField;
     private Text chatWindow;
 
     public override void OnStartLocalPlayer()
     {
         chatWindow = GameObject.Find("MessageLog").GetComponent<Text>();
         chatWindow.text += "Chat Activated";
         Debug.Log(chatWindow.text);
         Debug.Log("isLocalPlayer? "+isLocalPlayer);
         Debug.Log("isServer? " + isServer);
         Debug.Log("isClient? " + isClient);
         Debug.Log("hasAuthority? " + hasAuthority);
 
     }
 
     [Command]
     public void CmdSendMessage(string m)
     {
         RpcGetMessage(m);
     }
 
     [ClientRpc]
     public void RpcGetMessage(string m)
     {
         chatWindow.text += "\n" + m;
     }
 
     public void HandleMessage()
     {
         TextField = GameObject.Find("ChatField").GetComponent<InputField>();
         if (TextField.text != "")
         {
             if (isServer)
             {
                 RpcGetMessage(TextField.text);
             }
             else
             {
                 CmdSendMessage(TextField.text);
             }
             TextField.text = "";
         }
     }
 }
 

When I launch my little game as host, the debug functions in OnStartLocalPlayer run and tell me accurate information. Then when I put a message in my text field and press send. The chat fails with a warning stating: "Trying to send command for object without authority."

First of all, it shouldn't be sending a command from the host player. I put the "isServer" check for that very reason (which fails in the HandleMessage function).

Secondly, the player object which this script is on should have authority.

When I use a host and client setup nothing changes.

alt text

Notes: All the network setup is based on all the work up to this step in the Multiplayer Networking Tutorial: https://unity3d.com/learn/tutorials/topics/multiplayer-networking/death-and-respawning?playlist=29690

The Player Object has a Network Identity set to Local Authority.

I just added the Chat script to the player prefab and the 3 canvas elements: Text InputField Button - Calls HandleMessage. Defined by dragging the prefab player object into the button's OnClick list.


Update:

It seems that what was going on is that the button was referring to some 'other' version of the player object, not the particular instance that the user was using and/or ALL player objects.

I'm guessing it was referring to the static class or something of that nature, but I'm not familiar enough with Unity development to say.

However I'm now experiencing more issues, still causing me to be unable to send and receive messages:

When I'm on the host and I send a message, it skips the Command function call and goes straight to the Rpc call. It runs fine on the host game instance but nothing happens on the client instance. I have a (!isLocalPlayer) check before it updates the text field and whatnot so I'm assuming that gets switched and nothing happens. When I try to put a text field entry as a debug log BEFORE the (!isLocalPlayer) it says that the variable on that line is a null reference. I don't know why this isn't considered the local player and none of the variables are considered the same. (Is it because the function is being called on the HOSTS player object in my client instance?)

When I'm on the client and I send a message, it steps through all the steps up to Calling the Command function. Then on the host, nothing gets called.

authority.png (160.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by RamonLion · Mar 01, 2017 at 11:55 PM

I figured it out.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 using UnityEngine.UI;
 
 public class Chat : NetworkBehaviour
 {
     private InputField TextField;
     private Text chatWindow;
 
     public void Awake()
     {
         chatWindow = GameObject.Find("MessageLog").GetComponent<Text>();
         TextField = GameObject.Find("ChatField").GetComponent<InputField>();
     }
 
     public override void OnStartLocalPlayer()
     {
         chatWindow.text += "Chat Activated";
 
         Button btn = GameObject.Find("Send").GetComponent<Button>();
         btn.onClick.AddListener(HandleMessage);
     }
 
     [Command]
     public void CmdSendMessage(string m)
     {
         RpcGetMessage(m);
     }
 
     [ClientRpc]
     public void RpcGetMessage(string m)
     {        
         chatWindow.text += "\n" + m;
     }
 
     private void HandleMessage()
     {
         if (TextField.text != "")
         {
             if (isServer)
             {
                 RpcGetMessage(TextField.text);
             }
             else
             {
                 CmdSendMessage(TextField.text);
             }
             TextField.text = "";
         }
     }
 }
 

It was a scoping issue but not quite in the way I had interpreted.

The issue was that all the commands were being made by the local user's object. But then when the host was enacting Rpc commands it was being directed the the client side hosts game object. So while I was using the local player authority as an I/O I was supposed to make the local authority SEND the commands and then the hosts instance RECEIVE the return calls.

How agonizingly tedious. Is there any reason for this design choice?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

116 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I use an InputField to change a player object's variable? 1 Answer

How would I store local data on the server, without overwriting that server's own data? Essentially making a copy. 0 Answers

How to command dedicated server 1 Answer

Sending Command from client to server Using an UI button 0 Answers

[Command] Not being called at all 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges