Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Spawn GameObjects in a Network

Discussion in 'Multiplayer' started by NahuGuzmy, Sep 11, 2015.

  1. NahuGuzmy

    NahuGuzmy

    Joined:
    Aug 15, 2015
    Posts:
    14
    Hi, im trying to spawn GameObects in my multiplayer game, but i get this error in console:



    this is my code:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class Spawn : NetworkBehaviour {
    6.     public GameObject prefab;
    7.     public GameObject melee;
    8.     float timerWave;
    9.     float timerCreep;
    10.     public float timeBetweenCreep;
    11.     public float timeBetweenWave;
    12.     public int waveSize;
    13.     int j=0;
    14.     NetworkClient myClient;
    15.     // Use this for initialization
    16.     void Start ()
    17.     {
    18.         ClientScene.RegisterPrefab (prefab);
    19.         myClient = new NetworkClient();
    20.         //myClient.RegisterHandler (MsgType.Connect);
    21.         myClient.Connect ("localhost", 7777);
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update () {
    26.         timerWave += Time.deltaTime;
    27.         timerCreep += Time.deltaTime;
    28.         if (Input.GetKey (KeyCode.O) )
    29.         {
    30.             timerWave = timeBetweenWave;
    31.         }
    32.         if (timerWave >= timeBetweenWave) {
    33.             //timerCreep += Time.deltaTime;
    34.  
    35.             if(timerCreep >= timeBetweenCreep && j<=waveSize){
    36.  
    37.  
    38.                 Network.Instantiate (prefab, new Vector3 (transform.position.x, transform.position.y, transform.position.z), Quaternion.identity,0);
    39.                 //GameObject.Instantiate (melee, new Vector3 (transform.position.x, transform.position.y, transform.position.z), Quaternion.identity);
    40.              
    41.                 j++;
    42.                 timerCreep = 0f;
    43.                 //timerCreep=0f;
    44.  
    45.             }
    46.             if(j == waveSize)
    47.             {
    48.                 //timerCreep = 0f;
    49.                 timerWave = 0f;
    50.                 j=0;
    51.             }
    52.         }
    53.     }
    54. }
    I dont know what i am doing wrong
     
  2. NahuGuzmy

    NahuGuzmy

    Joined:
    Aug 15, 2015
    Posts:
    14
    [SOLVED] here is the right code:

    Code (CSharp):
    1. using UnityEngine.Networking;
    2.  
    3. public class Spawn : NetworkBehaviour {
    4.    
    5.     [SerializeField] GameObject prefab;
    6.     float timerWave;
    7.     float timerCreep;
    8.     public float timeBetweenCreep;
    9.     public float timeBetweenWave;
    10.     public int waveSize;
    11.     int j=0;
    12.  
    13.     // Use this for initialization
    14.     void Start ()
    15.     {
    16.  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update () {
    21.         timerWave += Time.deltaTime;
    22.         timerCreep += Time.deltaTime;
    23.         if (Input.GetKey (KeyCode.O) )
    24.         {
    25.             timerWave = timeBetweenWave;
    26.         }
    27.         if (timerWave >= timeBetweenWave) {
    28.  
    29.             if(timerCreep >= timeBetweenCreep && j<=waveSize && isServer){
    30.  
    31.                 GameObject pre1 = GameObject.Instantiate (prefab, new Vector3 (transform.position.x, transform.position.y, transform.position.z), Quaternion.identity) as GameObject;
    32.                 GameObject pre2 = GameObject.Instantiate (prefab, new Vector3 (transform.position.x, transform.position.y, transform.position.z), Quaternion.identity) as GameObject;
    33.  
    34.                 NetworkServer.Spawn(pre1);
    35.                 NetworkServer.Spawn(pre2);
    36.  
    37.                
    38.                 j++;
    39.                 timerCreep = 0f;
    40.                 //timerCreep=0f;
    41.  
    42.             }
    43.             if(j == waveSize)
    44.             {
    45.                 //timerCreep = 0f;
    46.                 timerWave = 0f;
    47.                 j=0;
    48.             }
    49.         }
    50.     }
    51. }
    52.  
     
  3. ritesh_khokhani

    ritesh_khokhani

    Joined:
    Jun 26, 2013
    Posts:
    47

    Hi,

    Why we can't use Network.Instantiate in this case as this is also seem to be right code as per my know ?
     
  4. TehGM

    TehGM

    Joined:
    Nov 15, 2013
    Posts:
    89
    Newtork.Instantiate is for old Unity networking. NetworkServer.Spawn is for new UNet introduced in 5.1.