Why wont this script network to client?
I am trying to have the localplayer tell the server to spawn a prefab and network it to all players. The prefab has network identity on it. My issue is the prefab wont spawn on the client (works as intended on the host/server). The script controlling the prefab spawning is as follows.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class SnowpileController : NetworkBehaviour {
public float spawnTime;
private float nextTime;
// private GameObject[] powerupArray;
public GameObject snowPile;
// Use this for initialization
void Start()
{
nextTime = Time.time+5;
CmdSnowballSpawn();
}
// Update is called once per frame
void Update()
{
if (Time.time > nextTime)
{
nextTime = nextTime + spawnTime;
Debug.Log("trying to spawn");
CmdSnowballSpawn();
}
}
[ClientRpc]
void RpcSnowballSpawn()
{
CmdSnowballSpawn();
}
[Command]
void CmdSnowballSpawn()
{
var objs = FindObjectsOfType<SnowpileSpawn>();
var spawnPos = objs[Random.Range(0, objs.Length)];
var randX = Random.Range(0.0f, 1.0f);
var randY = Random.Range(0.0f, 1.0f);
var bullet = (GameObject)Instantiate(
snowPile,
spawnPos.transform.position + (new Vector3(randX, randY, spawnPos.transform.position.z)),
spawnPos.transform.rotation);
NetworkServer.Spawn(bullet);
}
}
Answer by KPLloyd · Dec 09, 2016 at 05:07 AM
Ok, the reason this didn't want to work right was a combination of a few things. First, I was using isLocalPLayer on a NON-PLAYER object. I needed to use isClient instead. Secondly, the way I was placing the timer was a bit off. Finally, and not sure if this was necessary, I registered the prefab to the client. Here is my working script.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class SnowpileController : NetworkBehaviour {
public float spawnTime;
[SyncVar]
public float nextTime;
[SyncVar]
public float thisTime;
// private GameObject[] powerupArray;
public GameObject snowPile;
// Use this for initialization
void Start()
{
ClientScene.RegisterPrefab(snowPile);
if (isClient)
{
nextTime = Time.time;
}
// nextTime = Time.time;
//CmdSnowballSpawn();
}
// Update is called once per frame
void Update()
{
if (!isClient)
{
return;
}
thisTime = Time.time;
if (thisTime - nextTime > spawnTime)
{
nextTime = thisTime;
CmdSnowballSpawn();
}
}
[ClientRpc]
void RpcSnowballSpawn()
{
CmdSnowballSpawn();
}
[Command]
void CmdSnowballSpawn()
{
Debug.Log("trying to spawn");
var objs = FindObjectsOfType<SnowpileSpawn>();
var spawnPos = objs[Random.Range(0, objs.Length-1)];
var randX = Random.Range(0.0f, 1.0f);
var randY = Random.Range(0.0f, 1.0f);
var bullet = (GameObject)Instantiate(
snowPile,
spawnPos.transform.position + (new Vector3(randX, randY, spawnPos.transform.position.z)),
spawnPos.transform.rotation);
NetworkServer.Spawn(bullet);
}
}
Your answer
Follow this Question
Related Questions
uNet Spawning Objects 0 Answers
UNET Online Match Making? 1 Answer
Bullets not spawning in other clients but its spawning on the client that shot it. 1 Answer
local scale network 0 Answers