Search Unity

  1. We want you to join us at GDC this year! Take a peek at all of the events we will be hosting during the week of GDC.
    Dismiss Notice
  2. Tell us about your experience here and you’ll get early access to the 2018 Game Studios report + more goodies.
    Dismiss Notice
  3. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  5. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice

Assigning Authority

Discussion in 'Multiplayer Networking' started by awesomesid123, Feb 8, 2018.

  1. awesomesid123

    awesomesid123

    Joined:
    Oct 4, 2014
    Posts:
    20
    So I recently created a script in order to assign authority to players on when they come in contact with an interactable football. But the script shows errors. Please help.

    Script :

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Networking;

    public class authority : MonoBehaviour {
    public GameObject Obj;

    // Use this for initialization
    void Start () {
    Obj = GameObject.Find("Multiplayer_Player(Clone)");
    }


    // Update is called once per frame
    void Update () {

    }

    private void OnTriggerEnter(Collider other)
    {
    if (other.tag == "football")
    {
    Cmd_AssignLocalAuthority;
    }
    }
    private void OnTriggerExit(Collider other)
    {
    if (other.tag == "football")
    {
    if (other.tag == "football")
    {
    Cmd_RemoveLocalAuthority;
    }
    }
    }

    [Command]
    void Cmd_AssignLocalAuthority (GameObject Obj)
    {
    NetworkInstanceId nIns = Obj.GetComponent<NetworkIdentity> ().netId;
    GameObject client = NetworkServer.FindLocalObject (nIns);
    NetworkIdentity ni = client.GetComponent<NetworkIdentity> ();
    ni.AssignClientAuthority(client);
    }

    [Command]
    void Cmd_RemoveLocalAuthority (GameObject Obj) {
    NetworkInstanceId nIns = Obj.GetComponent<NetworkIdentity> ().netId;
    GameObject client = NetworkServer.FindLocalObject (nIns);
    NetworkIdentity ni = client.GetComponent<NetworkIdentity> ();
    ni.RemoveClientAuthority (client);
    }
    }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    1,396
    1) Use code tags on the forum to make your code readable, or few will even try reading it
    2) No one will help with code errors if you aren't going to say what the code errors are
    3) No one will help with errors if you aren't going to say what line number the error occurs
     
  3. awesomesid123

    awesomesid123

    Joined:
    Oct 4, 2014
    Posts:
    20
    The error occurs in the lines which say : "ni.RemoveClientAuthority (client);" and "ni.AssignClientAuthority(client);"
    The error is not shown in the console but whenever I try to build the game it says please fix all compiler errors. So now I hope you are in a better position of helping me out here :)
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    1,396
    You still haven't even said what the error is......

    It looks like you're calling both of your Command methods incorrectly though. They are declared as "void Cmd_RemoveLocalAuthority(GameObject Obj)" but you're not putting the () after or passing any GameObject.
     
  5. awesomesid123

    awesomesid123

    Joined:
    Oct 4, 2014
    Posts:
    20
    So I finally found out that the error I was getting was that whenever the object enters the collider it gives an error saying that authorities can only be assigned for server spawned objects. Thank you so much for your patience.