Search Unity

[SOLVED] Networking.Spawn how spawn object from server to all clients?

Discussion in 'Multiplayer' started by Rafael_Gratival, Aug 17, 2016.

  1. Rafael_Gratival

    Rafael_Gratival

    Joined:
    Oct 27, 2014
    Posts:
    9
    I've read all topics about networking , but still have not found a solution for this someone could help me ?
    Basically what I want is to spawn different monsters in different positions and rotations in server , and all clients sync them.

    Components on Object to spawn:



    in case image dont show : http://imgur.com/d8glNFP.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class EnemySpawner: NetworkBehaviour
    6. {
    7.     public GameObject[] monsterType;
    8.  
    9.     GameObject[] monsterInstantiate;
    10.     int randomizer;
    11.  
    12.     public override void OnStartServer()
    13.     {
    14.         monsterInstantiate = new GameObject[10];
    15.  
    16.         StartSpawner ();
    17.     }
    18.        
    19.     void StartSpawner()
    20.     {
    21.         for(int i = 0; i < 10; i++)
    22.         {
    23.             Vector3 Pos = new Vector3 (Random.Range (-30, 30), 0, Random.Range (-30, 30));
    24.  
    25.             Quaternion Rot = Quaternion.Euler (new Vector3 (0, Random.Range (0f, 360F), 0));
    26.  
    27.             randomizer = Random.Range (0, 9);
    28.             monsterInstantiate[i] = (GameObject)Instantiate (monsterType[randomizer], Pos, Rot);
    29.             NetworkServer.Spawn (monsterInstantiate[i]);
    30.         }
    31.     }
    32. }
     
  2. wobes

    wobes

    Joined:
    Mar 9, 2013
    Posts:
    763
    Did you add enemy prefab into Registered Objects on NetworkManager?
     
  3. Rafael_Gratival

    Rafael_Gratival

    Joined:
    Oct 27, 2014
    Posts:
    9
    Ou yes register but forget to save scene for build test , my bad and thanks!
     
  4. wobes

    wobes

    Joined:
    Mar 9, 2013
    Posts:
    763
    Np)
     
unityunity