- Home /
can't get clients to spawn object
I know there must be something I am missing. I have tried countless different solutions to this problem, but I cannot seem to get my Client players to spawn rockets in this first person game I am working on. I have seen countless similar questions and tried every little piece of advice I could find but with no success. I will start with the shoot rocket script as that is where my command method is located. I have this script attached to every player. The host player can spawn object just fine, but my clients just can't get these darn rockets to spawn.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.Networking;
public class ShootRocket : NetworkBehaviour {
public float speed = 10.0f; // how fast rocket shoots
public GameObject redRocket; //prefab of rocket object
public GameObject blueRocket;
//Variables relating to the score ball
public GameObject scoreBallPrefab; //The object that will be spawned when we let go of the right mouse button
public Slider chargeSlider; // the slider that is associated with shooting the basketball.
public float chargeSpeed = 10.0f; //how fast to fill up bar for basketball shot.
public float maxForce = 350.0f;
public Transform bulletSpawn;
public GameObject myCamera;
[Command]
public void Cmd_ShootARocket(){
if(isLocalPlayer)
{
if(gameObject.layer == LayerMask.NameToLayer("BlueTeam"))
{
GameObject tempRocket = Instantiate(blueRocket,
myCamera.transform.position,
Quaternion.LookRotation(myCamera.transform.forward)) as GameObject;
//NetworkServer.SpawnWithClientAuthority(tempRocket, connectionToClient);
NetworkServer.Spawn(tempRocket);
}
else{
GameObject tempRocket = Instantiate(redRocket,
myCamera.transform.position,
Quaternion.LookRotation(myCamera.transform.forward)) as GameObject;
//NetworkServer.SpawnWithClientAuthority(tempRocket, connectionToClient);
NetworkServer.Spawn(tempRocket);
}
}
}
// Update is called once per frame
void Update ()
{
if(!base.isLocalPlayer)
{
return;
}
if( Input.GetButtonDown("Fire1"))
{
Cmd_ShootARocket();
}
}
As far as I can tell, this is what everyone is recommending I do. I have tried changing my spawn method to spawnwithplayerauthority which has also failed me miserably. I am under the impression that this is the only script where the problem would be, but I will add a few more likely suspects below just in case. First, my player setup script which is ripped directly from brackeys besides some team changing nonesense I was working on today (I plan on cleaning that up in the near future) using UnityEngine; using System.Collections; using UnityEngine.Networking; using UnityEngine.UI;
/**
* Initial setup of player characters. Makes sure unnecessary components are turned off.
* Sets up the players in their appropriate team etc.
*/
public class PlayerSetup : NetworkBehaviour {
[SerializeField]
Behaviour[] componentsToDisable;
Camera sceneCamera;
public GameObject hudObj;
[SyncVar(hook = "OnColorChange")]
public Color myColor;
void Start () {
//Logic to turn off unnecessary components
if(!isLocalPlayer)
{
for(int curComponent = 0; curComponent < componentsToDisable.Length; curComponent++)
{
componentsToDisable[curComponent].enabled = false;
}
}else
{
sceneCamera = Camera.main;
if(sceneCamera != null)
{
sceneCamera.gameObject.SetActive(false);
}
GameObject hudInstance = GameObject.Instantiate(hudObj) as GameObject;
GetComponent<ShootRocket>().chargeSlider = hudInstance.GetComponentInChildren<Slider>();
}
}
void Update()
{
GetComponent<Renderer>().material.color = myColor;
if(myColor == Color.red)
{
gameObject.layer = LayerMask.NameToLayer("RedTeam");
}
else{
gameObject.layer = LayerMask.NameToLayer("BlueTeam");
}
}
public void OnColorChange(Color newColor){
myColor = newColor;
}
}
Finally, I will include my extended network manager because it is something I know less about in comparison to other topics using UnityEngine; using System.Collections; using UnityEngine.Networking;
public class NetworkExtension : NetworkManager {
public Transform testSpawnPnt;
public int userCount = 0;
public override void OnServerAddPlayer (NetworkConnection conn, short playerControllerId)
{
if(userCount % 2 == 1)
{
GameObject player = (GameObject)Instantiate(playerPrefab, testSpawnPnt.position, Quaternion.identity);
player.layer = LayerMask.NameToLayer("BlueTeam");
player.GetComponent<PlayerSetup>().myColor = Color.blue;
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
else
{
GameObject player = (GameObject)Instantiate(playerPrefab, testSpawnPnt.position, Quaternion.identity);
player.layer = LayerMask.NameToLayer("RedTeam");
player.GetComponent<PlayerSetup>().myColor = Color.red;
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
userCount ++;
}
public override void OnServerDisconnect (NetworkConnection conn)
{
base.OnServerDisconnect (conn);
userCount --;
}
}
Hopefully someone out there knows what the heck is going on. I can provide copious extra details if need be. Please help!
Thanks so much, Tristan
EDIT: I would like to point out that yes, I have registered both of the rocket prefabs and yes they both have network identity and network transform components.
Okay so Quick Update! I woke up this morning and realized that once the command starts running, the only thing that is running it is the server. Therefore, my problem was that I was checking for isLocalPlayer to be true in my command which would obviously always come up false! $$anonymous$$y command function now looks like this. However, I have now run into a problem that I had earlier which is that when I shoot a client as host, they are not being affected by the host's rocket on either instance! Very confusing but I will update again if I find the culprit.
[Command]
public void Cmd_ShootARocket(){
Debug.Log("$$anonymous$$ade it to command");
if(gameObject.layer == Layer$$anonymous$$ask.NameToLayer("BlueTeam"))
{
GameObject tempRocket = Instantiate(blueRocket,
myCamera.transform.position,
Quaternion.LookRotation(myCamera.transform.forward)) as GameObject;
//NetworkServer.SpawnWithClientAuthority(tempRocket, connectionToClient);
Debug.Log ("Created tempRocket");
Debug.Log (tempRocket.ToString());
Debug.Log ("is Client comes back as" + isClient);
NetworkServer.Spawn(tempRocket);
}
else{
GameObject tempRocket = Instantiate(redRocket,
myCamera.transform.position,
Quaternion.LookRotation(myCamera.transform.forward)) as GameObject;
//NetworkServer.SpawnWithClientAuthority(tempRocket, connectionToClient);
Debug.Log ("Created tempRocket");
Debug.Log (tempRocket.ToString());
Debug.Log ("is Client comes back as" + isClient);
NetworkServer.Spawn(tempRocket);
}
}
What do you mean by client not being affected by rocket? Does it have some script attached to work?