Search Unity

UI Buttons To Spawn Units/Buildings

Discussion in 'Multiplayer' started by earlington, Sep 23, 2015.

  1. earlington

    earlington

    Joined:
    Sep 30, 2014
    Posts:
    3
    Hi Folks,

    after nearly 30 hours of testing and failing I have to ask now.

    Is there any chance to have a MainGUI (Canvas) in the general hierachy for every client and to spawn units/buildings per button click from it?

    SpawnWithClientAuthority doesnt seem to work in this case, cause the main-gui isn't a child of the playerobject. So, i am getting the message: "no authority".

    Furthermore, when I make this GUI a child of the playerobject, the gui is rotating with the player cam.

    I am really stuck on this, so any help would be highly appreciated.
    Thanks Chris
     
  2. Huntibia

    Huntibia

    Joined:
    Feb 12, 2014
    Posts:
    14
    If you are using the Unity 4.6 UI tools, you can leave the whole UI setup in the scene (so every client will have it). Then, let's say you have a button and a regular monoscript function assigned to it to spawn a unit. All you have to do is make that function call a [Command] through the client player object (or another object with client authority) so the server can spawn the unit.
     
  3. TehGM

    TehGM

    Joined:
    Nov 15, 2013
    Posts:
    89
    You can also SpawnWithClientAuthority, but it'll need to have a NetworkBehaviour and NetworkIdentity components attached to it.
    You can also use Network Messages.
    Approach depends on your design.
     
  4. earlington

    earlington

    Joined:
    Sep 30, 2014
    Posts:
    3
    well,. ... maybe i'm dumb as hell, but i don't get it: i've started a complete new project with this hierachy:

    - NetManager (Networkmanager, HUD)
    - GUI --> Button (on click() GameManager --> Test.TestSpawn)
    - EventSystem
    - GameManager (Test, Network Identity, , authority unchecked)
    ____
    Playerprefab (Network Identity, authority checked)
    HousePrefab (Network Identity, authority checked)

    and here is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class Test : NetworkBehaviour {
    6.  
    7.     public GameObject housePrefab;
    8.  
    9.     public void TestSpawn()
    10.     {
    11.        CmdSpawn();
    12.     }
    13.  
    14.     [Command]
    15.     public void CmdSpawn()
    16.     {
    17.        GameObject go = Instantiate(housePrefab);
    18.         NetworkServer.Spawn(go);                     // working on host; on client: "Trying to send command for object without authority."
    19.        //NetworkServer.SpawnWithClientAuthority(housePrefab, connectionToClient); // host working, but: "owner cannot be null"; on client: "Trying to send command for object without authority."
    20.     }
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class PlayerSetup : NetworkBehaviour {
    6.  
    7.     public Camera playerCamera;
    8.     public AudioListener audioListen;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         if (!isLocalPlayer)
    13.             return;
    14.  
    15.         //playerCamera.enabled = true;
    16.         audioListen.enabled = true;
    17.  
    18.         //GameObject.Find("SceneCamera").SetActive(false);
    19.     }
    20. }

    Doesnt matter what i try, it simply doesnt work.
    Greetz
     

    Attached Files:

    • 01.png
      01.png
      File size:
      129.8 KB
      Views:
      1,582
  5. Huntibia

    Huntibia

    Joined:
    Feb 12, 2014
    Posts:
    14
    See the attached zip for an example of how to do it. The only thing that I forgot to mention is that you also need to check "Local Player Authority" on the prefab that you're trying to spawn with client authority.

    When you click on "Spawn!", it will spawn a cube on the server (and therefore on all clients) with client authority only on the client you pressed the button.
     

    Attached Files:

  6. earlington

    earlington

    Joined:
    Sep 30, 2014
    Posts:
    3
    Fantastic !! :)
    Thank you so much !
     
  7. LeopardX

    LeopardX

    Joined:
    May 31, 2015
    Posts:
    64
    That project file has no assets in it its a blank project for me ?
     
  8. Huntibia

    Huntibia

    Joined:
    Feb 12, 2014
    Posts:
    14
    It's just two scenes and a couple of scripts to explain one way to do it.
     
  9. LeopardX

    LeopardX

    Joined:
    May 31, 2015
    Posts:
    64
    What unity version was it made in because its just an empty project for me
    Yea just checked the archive and assets the files are there, but when you load the project in unity 5.2 the project is empty
     
  10. Huntibia

    Huntibia

    Joined:
    Feb 12, 2014
    Posts:
    14
    I just opened in 5.2.1 and it's ok. Make sure you're selecting the folder which contains the assets and project settings folder, not the parent folder.
     
  11. LeopardX

    LeopardX

    Joined:
    May 31, 2015
    Posts:
    64
    Not sure what it is, but its added its self to the unity launch window as a project, still empty, but since i can brows the files manually i was able to load it in that way.
     
  12. DonTiki

    DonTiki

    Joined:
    Jun 21, 2016
    Posts:
    1
    Yes, after 2 days of trying to network spawn gameobjects from the UI, i finally managed to do it.
    Here is all the requirements:
    • UI element needs to have Network Identity Component
    • UI element needs to have a Script attached to it where you reference Script which is attached to the player prefab
    • On the Event Trigger of the UI image, reference itself(the UI image) and trigger the function which calls the command on the player object
    • On the prefab that you are trying to instantiate you need to add Network Identity component and mark "Local player authority"
    • in OnStartLocalPlayer() method on the player's script reference the ui element GameObject.Find ("UIelement").GetComponent<UIShooting> ().player = this;
    • You should use this method for instantiation NetworkServer.SpawnWithClientAuthority(bullet, this.gameobject) and not NetworkServer.Spawn(bullet)
    I advise you to Just check how Huntibia did it in TestSpawn.rar file above.
     
    Last edited: Jun 21, 2016