I have been fiddling with solutions on this one for a few days without success. I have a prefab set up in the Network Manager’s “Registered Spawnable Prefabs” section. However I keep getting errors when I try and spawn it.
This is the function that spawns the prefab.
#pragma strict
import UnityEngine.Networking;
public class SpawnScript extends NetworkBehaviour {
private var piecePrefab : GameObject;
public var pcId : int = 2000;
function NetSpawn () {
var piecePrefab : GameObject = Instantiate(Resources.Load(pcId.ToString()));
NetworkServer.SpawnWithClientAuthority(piecePrefab, connectionToClient);
}
}
However when I run it it doesn’t run as client or authority. Here is the Network Information.
And these are the errors I get when I try and spawn the prefab:
Error 1:
There is no NetworkIdentity on this object. Please add one.
UnityEngine.Networking.NetworkBehaviour:get_connectionToClient()
SpawnScript:NetSpawn() (at Assets/Standard Assets/2D/Scripts/SpawnScript.js:286)
$:MoveNext() (at Assets/Standard Assets/2D/Scripts/SpawnScript.js:62)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
Error 2:
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.Networking.NetworkBehaviour.get_connectionToClient () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkBehaviour.cs:25)
SpawnScript.NetSpawn () (at Assets/Standard Assets/2D/Scripts/SpawnScript.js:286)
SpawnScript+$Start$23+$.MoveNext () (at Assets/Standard Assets/2D/Scripts/SpawnScript.js:62)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
Any help would be greatly appreciated. Thank you in advance.