NetworkServer.Spawn() not being called
My I have a command that runs on a player object that spawns a bullet prefab on the server and sets a bunch of its variables to different things but it isn't being spawned when I call NetworkServer.Spawn(bullet);
I have checked and it is being registered, in both the inspector and I tried in code to check to see if I registered it incorrectly but that isn't the issue. I know that it's not being called because I can pause the game, go to the Network Identity and click spawn then the bullet works as intended.
Here is my code:
[Command] void CmdspawnBullet(Vector3 parent, float XPosOff,float ZPosOff, float XNegOff, float ZNegOff, Vector3 scaleFactor, float XSpeed, float YSpeed, float ZSpeed, bool Mover, Vector3 bulletTarget) { //set the transform of the bullet then instantiate and spawn GameObject bullet = (GameObject)Resources.Load ("bullet"); print ("Spawn " + bullet); //bullet.SetActive (true); Vector3 bulletPos = new Vector3(parent.x + XPosOff + XNegOff, parent.y, parent.z + ZNegOff + ZPosOff); ClientScene.RegisterPrefab(bullet); GameObject.Instantiate(bullet, bulletPos, Quaternion.identity); NetworkServer.Spawn (bullet); bullet.transform.LookAt(bulletTarget); bullet.transform.position = bulletPos; bullet.transform.localScale = scaleFactor; bullet.GetComponent<BulletScript>().speedZ = ZSpeed; bullet.GetComponent<BulletScript>().speedY = YSpeed; bullet.GetComponent<BulletScript>().speedX = XSpeed; bullet.GetComponent<BulletScript>().doWeMove = Mover; }
Answer by fredfishgames · Apr 05, 2017 at 04:04 PM
You need to do
GameObject _bullet = (GameObject)Instantiate(bullet, bulletPos, Quaternion.identity); ClientScene.RegisterPrefab(bullet); NetworkServer.Spawn(_bullet)
The bullet you instantiated must be spawned, not the prefab. This might also have to be called on the server, so you might want to check that before running this.
Hope this helps
Your answer
Follow this Question
Related Questions
What does this error mean? 1 Answer
[UNet] Instantiating over Network won't work 0 Answers
Unity Photon Handle Match Data 0 Answers
NetworkServer is not active. Cannot spawn objects without an active server. 0 Answers
NetworkServer.Spawn() only on Server (with registered prefabs) [Unity 5.2.3f1] 0 Answers