Search Unity

How to make this character select UNET

Discussion in 'UNet' started by MTDues, Nov 13, 2018.

  1. MTDues

    MTDues

    Joined:
    Mar 27, 2014
    Posts:
    27
    im new to UNET coding, but im learning alot , how i can use this script , make it functional ?

    the codes are from :
    https://forum.unity.com/threads/usi...fdtBnwpFj2kH_pNhVJFLnkvtrHJWPXHn3RurEDpMoPVeo

    Code (CSharp):
    1.     using UnityEngine;
    2.     using UnityEngine.Networking;
    3.     using UnityEngine.Networking.NetworkSystem;
    4.    
    5.     public class NetworkCustom : NetworkManager
    6.     {
    7.    
    8.         public int chosenCharacter = 0;
    9.         public GameObject[] characters;
    10.    
    11.         //subclass for sending network messages
    12.         public class NetworkMessage : MessageBase
    13.         {
    14.             public int chosenClass;
    15.         }
    16.    
    17.         public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId, NetworkReader extraMessageReader)
    18.         {
    19.             NetworkMessage message = extraMessageReader.ReadMessage<NetworkMessage>();
    20.             int selectedClass = message.chosenClass;
    21.             Debug.Log("server add with message " + selectedClass);
    22.    
    23.             GameObject player;
    24.             Transform startPos = GetStartPosition();
    25.    
    26.             if(startPos != null)
    27.             {
    28.                 player = Instantiate(characters[chosenCharacter], startPos.position,startPos.rotation)as GameObject;
    29.             }
    30.             else
    31.             {
    32.                 player = Instantiate(characters[chosenCharacter], Vector3.zero, Quaternion.identity) as GameObject;
    33.    
    34.             }
    35.    
    36.             NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    37.    
    38.         }
    39.    
    40.         public override void OnClientConnect(NetworkConnection conn)
    41.         {
    42.             NetworkMessage test = new NetworkMessage();
    43.             test.chosenClass = chosenCharacter;
    44.    
    45.             ClientScene.AddPlayer(conn, 0, test);
    46.         }
    47.    
    48.    
    49.         public override void OnClientSceneChanged(NetworkConnection conn)
    50.         {
    51.             //base.OnClientSceneChanged(conn);
    52.         }
    53.    
    54.         public void btn1()
    55.         {
    56.             chosenCharacter = 0;
    57.         }
    58.    
    59.         public void btn2()
    60.         {
    61.             chosenCharacter = 1;
    62.         }
    63.     }
    64.    
    65.  
    and this code

    Code (CSharp):
    1.     using UnityEngine;
    2.     using System.Collections;
    3.     using UnityEngine.Networking;
    4.    
    5.     public class ChooseHero : MonoBehaviour {
    6.    
    7.         public GameObject characterSelect;
    8.    
    9.         public void PickHero(int hero)
    10.         {
    11.             NetworkManager.singleton.GetComponent<NetworkCustom>().chosenCharacter = hero;
    12.             characterSelect.SetActive(false);
    13.         }
    14.     }
    15.    
    16.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Since Unet is deprecated and being removed from the engine in a matter of months, now is not a good time to start learning it.

    As per your question, you should say what is going wrong with the script when you use it. On a quick look, nothing appears to be obviously non-functional.
     
  3. MTDues

    MTDues

    Joined:
    Mar 27, 2014
    Posts:
    27
    i didnt know UNET is about to vanish :/ i just got started learning networking , should i learn Photon instead? or would you recommend anything else?
     
  4. Jos-Yule

    Jos-Yule

    Joined:
    Sep 17, 2012
    Posts:
    292
    Joe-Censored likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Well for the details see the big sticky thread titled "UNet Deprecation Thread" about Unet's imminent removal. I'd link it, but it is hard to miss.

    What you should use depends on your game and what 3rd party services you'd also like. If you're building a dedicated server game, then Mirror would likely be a good choice. If you want a matchmaker system and clients acting as hosts, you'll have to roll your own matchmaker server and a service to connect clients hosting behind a firewall (Unity Multiplayer Service does this for Unet, but it is also deprecated, so don't start a project now that uses it). Photon provides those services.
     
    MTDues likes this.