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

Diffrent Spawn Objects In Multiplayer

Discussion in 'Multiplayer' started by UnityUser9860, Mar 13, 2016.

  1. UnityUser9860

    UnityUser9860

    Joined:
    Dec 8, 2015
    Posts:
    179
    Hi

    Im using unet to make a multiplayer sandbox game and both the players can move around and place blocks howether they can only place 1 type of block ive tried to do a system were when you pressed 1 2 or 3 it would select a block and when you placed the block it would be that block howether it only apeared on the person who didnt place the blocks screen and te person who did got a error saying it failed to spawn the asset then a bunch of numbers could i spawn the object but change its material acording to the block type anyway thank you

    using UnityEngine;
    using UnityEngine.Networking;

    publicclassPlace:NetworkBehaviour{

    publicGameObjectCube;
    publicGameObjectCube2;
    publicGameObjectPlaceOBJ;
    publicinttype=1;

    publicoverridevoidOnStartServer()
    {
    print("ServerOnline.");

    }
    voidUpdate(){
    if(isLocalPlayer){
    if(Input.GetKeyDown(KeyCode.Alpha1)){
    type=1;
    }
    if(Input.GetKeyDown(KeyCode.Alpha2)){
    type=2;
    }
    if(Input.GetKeyDown(KeyCode.Alpha3)){
    type=3;
    }

    if(Input.GetKeyDown(KeyCode.Mouse1))
    {
    if(type==1){
    Cmdplaceblock();
    }
    if(type==2){
    Cmdplaceblock2();
    }
    if(type==3){
    Cmdplaceblock3();
    }

    }


    }
    }

    [Command]
    voidCmdplaceblock(){

    varenemy=(GameObject)Instantiate(Cube,PlaceOBJ.transform.position,Quaternion.identity);

    NetworkServer.Spawn(enemy);




    }
    [Command]
    voidCmdplaceblock2(){

    varenemy=(GameObject)Instantiate(Cube,PlaceOBJ.transform.position,Quaternion.identity);

    NetworkServer.Spawn(enemy);




    }

    [Command]
    voidCmdplaceblock3(){

    varenemy=(GameObject)Instantiate(Cube,PlaceOBJ.transform.position,Quaternion.identity);

    NetworkServer.Spawn(enemy);




    }


    }
     
  2. sovium

    sovium

    Joined:
    Mar 8, 2016
    Posts:
    27
    Have you registered all of your prefabs as spawnable objects in the NetworkManager?

    Also, instead of doing what you are doing, you could just do:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4.  
    5. public class Place:NetworkBehaviour{
    6.  
    7.     public GameObject Cube;
    8.     public GameObject Cube2;
    9.     public GameObject PlaceOBJ;
    10.     public int type=1;
    11.  
    12.     public override void OnStartServer()
    13.     {
    14.         print("ServerOnline.");
    15.     }
    16.  
    17.     void Update(){
    18.  
    19.         if(isLocalPlayer){
    20.  
    21.             if(Input.GetKeyDown(KeyCode.Alpha1)){
    22.                 type=1;
    23.             }
    24.             if(Input.GetKeyDown(KeyCode.Alpha2)){
    25.                 type=2;
    26.             }
    27.             if(Input.GetKeyDown(KeyCode.Alpha3)){
    28.                 type=3;
    29.             }
    30.             if(Input.GetKeyDown(KeyCode.Mouse1)){
    31.                 CmdPlaceBlock(type);
    32.             }
    33.         }
    34.     }
    35.  
    36.     [Command]
    37.     void CmdPlaceBlock(int blockType){
    38.         var enemy = null;
    39.  
    40.         if(blockType == 1){
    41.             enemy = Instantiate(Cube,PlaceOBJ.transform.position,Quaternion.identity) as GameObject;
    42.          }else if(blockType == 2){
    43.             enemy = Instantiate(Cube2,PlaceOBJ.transform.position,Quaternion.identity) as GameObject;
    44.          }
    45.        
    46.          NetworkServer.Spawn(enemy);
    47. }
    48.  
     
  3. UnityUser9860

    UnityUser9860

    Joined:
    Dec 8, 2015
    Posts:
    179
    Thank You So Much
    Ive not put the cube in the accepted prefabs (facepalm) and the new script works great! thank you! @sovium
     
    Last edited: Mar 13, 2016
    sovium likes this.