- Home /
UNET Player Object Creation Error: Failed to spawn server object
I am attempting to create a player object for a connecting client. I have set the PlayerPrefab variable to be an empty object with a NetworkIdentity attached to it. The issue is that once the client connects, the player object is created on the server but the client gets this error:
Failed to spawn server object
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate().
I am not sure if there is something that I have set up incorrectly on the server or client in order to get this error. The client does connect to the server and switch scenes successfully just before this happens.
Answer by CommandoJones · Jun 25, 2015 at 12:45 PM
So for anyone else dealing with this problem, I found my solution. I have my client and server code in two separate projects which was an issue because the player prefabs for the client and server must share the same meta file in order to work. This could also be the case for any networked object someone might want to create through NetworkServer.Spawn() but I have not tested it personally. Good luck to anyone dealing with a similar issue.
CommandoJones, could you please share the demo project? I have issues with spawning..
Hey iyakov, unfortunately I don't have a small demo project to share. I found this issue while working on a rather large code base. However I can say that my particular issue with spawning a player object happened because I have two separate projects. One for the server and one for the client.
So when I assigned my player objects, the prefabs I was using did not share the same meta data which caused issues. If you are having a similar issue but you have one project for both your server and client code then I am not entirely sure what may be the cause.
I hope this helps!
$$anonymous$$ay be you may note my mistake:
Projects running on 2 UNITYs:
One project for a client;
One project for a server;
Each project has a GameObject with a Network$$anonymous$$anager;
Prefab:
The same meta, the same settings (Local Player Authority);
The prefab has NetworkIdentity And NetworkTransform;
client code:
public class DemoBehaviour : $$anonymous$$onoBehaviour
{
public Network$$anonymous$$anager $$anonymous$$anager; // Link to the same GO's component
public GameObject DemoPrefab; // Link to the spawn prefab
// Use this for initialization
void Start ()
{
$$anonymous$$anager.StartClient();
ClientScene.RegisterPrefab(DemoPrefab);
}
void OnGUI()
{
// this spawns only on client
if (GUILayout.Button("Create"))
{
GameObject obj = GameObject.Instantiate(DemoPrefab);
NetworkServer.Spawn(obj);
}
}
}
server code:
public class DemoBehaviour : $$anonymous$$onoBehaviour
{
public Network$$anonymous$$anager $$anonymous$$anager;
public GameObject DemoPrefab;
// Use this for initialization
void Start()
{
$$anonymous$$anager.StartHost();
ClientScene.RegisterPrefab(DemoPrefab);
}
void OnGUI()
{
// this spawns on both client & server. Only server can control
if (GUILayout.Button("Create"))
{
GameObject obj = GameObject.Instantiate(DemoPrefab);
NetworkServer.Spawn(obj);
}
}
}
Oh I see, are you getting the, Failed to spawn server object, error as well? I have not messed with NetworkServer.Spawn myself yet. I have only used the automatic PlayerObject spawning. However I am fairly certain that you don't need the OnGUI() code on the client since you can only spawn networked objects through the server. Going based off of the documentation below in the, Object Creation Flow, section.
Other than that as far as I can tell you haven't actually connected the server to the client. Unless you just didn't include that in your code snippet, in which case I am not entirely sure what is the issue. What you have shown me looks pretty straight forward.
http://docs.unity3d.com/$$anonymous$$anual/UNetSpawning.html
Oh, it's a pity. Btw, I don't use a custom connect, so a single call to Network$$anonymous$$anager.StartClient calls connect, ready and addplayer methods.
Please note:
this spawns on both client & server. Only server can control it
When I create a prefab at the server, it creates at the client side! However, only server can move the object so the client see its movement. But not vice versa. And I'm going crazy about this.