Hi, im trying to spawn GameObects in my multiplayer game, but i get this error in console:
this is my code:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class Spawn : NetworkBehaviour {
public GameObject prefab;
public GameObject melee;
float timerWave;
float timerCreep;
public float timeBetweenCreep;
public float timeBetweenWave;
public int waveSize;
int j=0;
NetworkClient myClient;
// Use this for initialization
void Start ()
{
ClientScene.RegisterPrefab (prefab);
myClient = new NetworkClient();
//myClient.RegisterHandler (MsgType.Connect);
myClient.Connect ("localhost", 7777);
}
// Update is called once per frame
void Update () {
timerWave += Time.deltaTime;
timerCreep += Time.deltaTime;
if (Input.GetKey (KeyCode.O) )
{
timerWave = timeBetweenWave;
}
if (timerWave >= timeBetweenWave) {
//timerCreep += Time.deltaTime;
if(timerCreep >= timeBetweenCreep && j<=waveSize){
Network.Instantiate (prefab, new Vector3 (transform.position.x, transform.position.y, transform.position.z), Quaternion.identity,0);
//GameObject.Instantiate (melee, new Vector3 (transform.position.x, transform.position.y, transform.position.z), Quaternion.identity);
j++;
timerCreep = 0f;
//timerCreep=0f;
}
if(j == waveSize)
{
//timerCreep = 0f;
timerWave = 0f;
j=0;
}
}
}
}
I dont know what i am doing wrong
[SOLVED] here is the right code:
using UnityEngine.Networking;
public class Spawn : NetworkBehaviour {
[SerializeField] GameObject prefab;
float timerWave;
float timerCreep;
public float timeBetweenCreep;
public float timeBetweenWave;
public int waveSize;
int j=0;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update () {
timerWave += Time.deltaTime;
timerCreep += Time.deltaTime;
if (Input.GetKey (KeyCode.O) )
{
timerWave = timeBetweenWave;
}
if (timerWave >= timeBetweenWave) {
if(timerCreep >= timeBetweenCreep && j<=waveSize && isServer){
GameObject pre1 = GameObject.Instantiate (prefab, new Vector3 (transform.position.x, transform.position.y, transform.position.z), Quaternion.identity) as GameObject;
GameObject pre2 = GameObject.Instantiate (prefab, new Vector3 (transform.position.x, transform.position.y, transform.position.z), Quaternion.identity) as GameObject;
NetworkServer.Spawn(pre1);
NetworkServer.Spawn(pre2);
j++;
timerCreep = 0f;
//timerCreep=0f;
}
if(j == waveSize)
{
//timerCreep = 0f;
timerWave = 0f;
j=0;
}
}
}
}
Hi,
Why we can’t use Network.Instantiate in this case as this is also seem to be right code as per my know ?
TehGM
4
Newtork.Instantiate is for old Unity networking. NetworkServer.Spawn is for new UNet introduced in 5.1.