- Home /
Client unable to retain reference to prefab
I have am creating a multiplayer game in unity and the player is suppose to spawn a simple spell prefab when the mouse button is clicked. However the client game always loses its reference to the prefab on load so it can not spawn it. This is my cast code
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class Cast : NetworkBehaviour {
public GameObject spell1;
public GameObject spell2;
private float nextcast = 0f;
void Start () {
if (!isLocalPlayer) {
return;
}
ClientScene.RegisterPrefab(spell1);
ClientScene.RegisterPrefab(spell2);
}
void Update () {
if (!isLocalPlayer) {
return;
}
if ((Input.GetMouseButtonDown (0)) && nextcast < Time.time) {
CmdCastSpell (spell1);
}
if ((Input.GetMouseButtonDown (1)) && nextcast < Time.time) {
CmdCastSpell (spell2);
}
}
[Command]
void CmdCastSpell(GameObject spell){
Debug.Log ("Casting");
Debug.Log (spell);
GameObject s = (GameObject) Instantiate (spell, transform.position + transform.forward * 2, transform.rotation);
NetworkServer.Spawn (s);
}
}
The server retains its reference to the prefab in spell1 and spell2 fine and sees the client player's references to these variables aswell however when ever i click with the client game the console prints out ( from the CMDCastSpell function)
Casting
Null
ArgumentException: The Object you want to instantiate is null.
I have tried dynamically finding the prefab at runtime from other game objects as well as hardcoding in the name of the prefab and loading from resources. I have also saved the prefab to the game object directly from the editor but all of these give the null reference error. I am new to unity networking so I am unsure what I'm doing wrong
Your answer
