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
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.
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.
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class Test : NetworkBehaviour {
public GameObject housePrefab;
public void TestSpawn()
{
CmdSpawn();
}
[Command]
public void CmdSpawn()
{
GameObject go = Instantiate(housePrefab);
NetworkServer.Spawn(go); // working on host; on client: "Trying to send command for object without authority."
//NetworkServer.SpawnWithClientAuthority(housePrefab, connectionToClient); // host working, but: "owner cannot be null"; on client: "Trying to send command for object without authority."
}
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class PlayerSetup : NetworkBehaviour {
public Camera playerCamera;
public AudioListener audioListen;
// Use this for initialization
void Start () {
if (!isLocalPlayer)
return;
//playerCamera.enabled = true;
audioListen.enabled = true;
//GameObject.Find("SceneCamera").SetActive(false);
}
}
Doesnt matter what i try, it simply doesnt work.
Greetz
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.
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.
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.