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. Dismiss Notice

Multiplayer Spawn Problem

Discussion in 'Editor & General Support' started by EdgarCarrera, Aug 31, 2014.

  1. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    in one client ( First client i can see two player and if i move 1 all players move. and second client only show one player. i think network not work?
    My script: Login and Spawn. ( i have character creation area. and work good. if u have character u go automatically to spawn. ) but how i can fix this?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class Networking : MonoBehaviour
    6. {
    7.     //Formularios
    8.     public InputField username;
    9.     public InputField password;
    10.     public InputField regUsername;
    11.     public InputField regPassword;
    12.     public InputField regRepeatPassword;
    13.     public InputField email;
    14.     public InputField numberInput;
    15.     public Text message;
    16.     public Text generatedNumber;
    17.     private int randomNumber;
    18.  
    19.     //Menu Actual
    20.     private string _CurrentMenu = "MenuLogin";
    21.  
    22.     //Desactivo GameObjects
    23.     public GameObject menuLogin;
    24.     public GameObject menuRegister;
    25.  
    26.     //Server Status AREA!
    27.     public Text serverStatus;
    28.     public GameObject menuLoginButton;
    29.  
    30.     //Chequea si tiene personaje
    31.     private int existCharacter;
    32.  
    33.     public static int UserID;
    34.  
    35.     void Start()
    36.     {
    37.         currentMenu ();
    38.         message.text = "";
    39.     }
    40.  
    41.     void Update()
    42.     {
    43.         ServerCheck ();
    44.         currentMenu ();
    45.     }
    46.  
    47.     private void currentMenu()
    48.     {
    49.         if (_CurrentMenu == "MenuLogin")
    50.         {
    51.             menuLogin.gameObject.SetActive(true); // Activo Script menuLogin
    52.             menuRegister.gameObject.SetActive(false); // Desactivo Script menuRegister
    53.  
    54.             ServerCheck();
    55.         }
    56.  
    57.         if (_CurrentMenu == "MenuRegister")
    58.         {
    59.             menuLogin.gameObject.SetActive(false); // Desactivo Script menuLogin
    60.             menuRegister.gameObject.SetActive(true); // Activo Script menuRegister
    61.      
    62.             generatedNumber.text = randomNumber.ToString();
    63.  
    64.         }
    65.     }
    66.  
    67.     public void loadLogin ()
    68.     {
    69.         _CurrentMenu = "MenuLogin";
    70.         message.text = "";
    71.         currentMenu ();
    72.     }
    73.  
    74.     public void loadRegister ()
    75.     {
    76.         _CurrentMenu = "MenuRegister";
    77.         randomNumber = Random.Range(1000,99999);
    78.         regUsername.value = "";
    79.         regPassword.value = "";
    80.         regRepeatPassword.value = "";
    81.         email.value = "";
    82.         numberInput.value = "";
    83.         message.text = "";
    84.         currentMenu ();
    85.     }
    86.  
    87.     public void regCheck()
    88.     {
    89.         message.text = "";  
    90.         if(regUsername.value == "" || email.value == "" || regPassword.value == "" || numberInput.value == "0"){
    91.             message.text += "Please fill in the empty fields. \n";
    92.         }else{
    93.             if(regPassword.value == regRepeatPassword.value){
    94.                 if(numberInput.value == generatedNumber.text){
    95.                     StartCoroutine("doRegister");
    96.                 }else{
    97.                     message.text += "The number you inputed is not the same number.";
    98.                 }
    99.              
    100.             }else{
    101.                 message.text += "The passwords do not match. \n";
    102.             }
    103.         }
    104.     }
    105.  
    106.     public void logCheck()
    107.     {
    108.         if(username.value == "" || password.value == ""){
    109.             message.text += "Please enter the necessary data! \n";
    110.         } else{
    111.             message.text = "";
    112.             StartCoroutine("doLogin");
    113.         }
    114.     }
    115.  
    116.     private void ServerCheck()
    117.     {
    118.         StartCoroutine (ServerRefresh ());
    119.              
    120.         if(MasterServer.PollHostList().Length != 0)
    121.         {
    122.             HostData[] data = MasterServer.PollHostList();
    123.             foreach(HostData c in data)
    124.             {
    125.                 serverStatus.text = c.gameName + " " + (c.connectedPlayers - 1) + "/" + c.playerLimit;
    126.                 menuLoginButton.gameObject.SetActive(true); // Activo Script menuLoginButton
    127.             }
    128.         }
    129.         else
    130.         {
    131.             serverStatus.text = "Server Offline";
    132.             menuLoginButton.gameObject.SetActive(false); // Desactivo Script menuLoginButton
    133.         }
    134.     }
    135.  
    136.     public void OnConnectedToServer()
    137.     {
    138.         networkView.RPC("LoginRPC",RPCMode.All, username.value, Network.player);
    139.  
    140.         LevelManager.Load ("LoadingData", 0f);
    141.     }
    142.  
    143.     private void OnDisconnectedFromServer()
    144.     {
    145.         _CurrentMenu = "MenuLogin";
    146.     }
    147.  
    148.     private IEnumerator doRegister()
    149.     {
    150.         WWWForm form = new WWWForm();
    151.         form.AddField("Username" , regUsername.value);
    152.         form.AddField("Password" , regPassword.value);
    153.         form.AddField("Email" , email.value);          
    154.         var w = new WWW(Info.webSite + "/Register.php", form);          
    155.         yield return w;
    156.         if(w.error == null){
    157.             message.text += w.text;
    158.  
    159.             yield return new WaitForSeconds(2);
    160.  
    161.             _CurrentMenu = "MenuLogin";
    162.  
    163.             StopCoroutine("doRegister");
    164.         }else{
    165.             message.text += "Error : " + w.error + "\n";
    166.         }
    167.     }
    168.     private IEnumerator doLogin()
    169.     {
    170.         WWWForm logform = new WWWForm();
    171.         logform.AddField("Username" , username.value);
    172.         logform.AddField("Password" , password.value);
    173.         var logw = new WWW(Info.webSite + "/Login.php", logform);
    174.         yield return logw;
    175.         if(logw.error == null){
    176.             message.text += logw.text;
    177.          
    178.             StopCoroutine("doLogin");
    179.         }else{
    180.             message.text +="Error : " + logw.error + "\n";  
    181.         }
    182.         if(message.text == "Login success! Please wait while the game loads..."){
    183.             Network.Connect("avct.sytes.net", 8632);
    184.         }
    185.     }
    186.  
    187.     private IEnumerator ServerRefresh()
    188.     {
    189.         yield return new WaitForSeconds(3);
    190.         MasterServer.RequestHostList("MMOFPS");
    191.     }
    192.  
    193.     [RPC]
    194.     public void LoginRPC(string username,NetworkPlayer player)
    195.     {
    196.         Info.Owner = username;
    197.     }
    198. }

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameStarted : MonoBehaviour {
    5.  
    6.     private const string MALE_MODEL_PATH = "Characters/Male/";
    7.     private const string FEMALE_MODEL_PATH = "Characters/Female/";
    8.     public string[] maleModels;
    9.     public string[] femaleModels;
    10.  
    11.     private int _index = 0;
    12.     public GameObject playerPref;
    13.  
    14.  
    15.     void OnServerInitialized()
    16.     {
    17.         //SpawnPlayer();
    18.     }
    19.  
    20.     void OnConnectedToServer()
    21.     {
    22.         //SpawnPlayer();
    23.     }
    24.     // Use this for initialization
    25.     void Start () {
    26.  
    27.         if(Info.charGender == 0)
    28.         {
    29.             playerPref = Network.Instantiate (Resources.Load (MALE_MODEL_PATH + maleModels[ _index ]), new Vector3(Info.charPosX, Info.charPosY, Info.charPosZ), transform.rotation, 0) as GameObject;
    30.         }
    31.         else if(Info.charGender == 1)
    32.         {
    33.             playerPref = Network.Instantiate (Resources.Load (FEMALE_MODEL_PATH + femaleModels[ _index ]), new Vector3(Info.charPosX, Info.charPosY, Info.charPosZ), transform.rotation, 0) as GameObject;
    34.         }
    35.  
    36.  
    37.     }
    38. }

     
  2. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
  3. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
  4. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
  5. Stankiem

    Stankiem

    Joined:
    Dec 4, 2013
    Posts:
    115
    No need for a second thread friend. Your question is so vague and I am not familiar with your code, it would take me a long time to sift through it. I hate to be this guy, but I feel like if you've been spending a couple weeks trying to get this to work, you probably need to start with something a bit simpler and try to get an easier project before tackling this one =)
     
  6. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    i know how i can code my problem is on networking
     
  7. Stankiem

    Stankiem

    Joined:
    Dec 4, 2013
    Posts:
    115
    Where is the character controller and spawning code? it seems like from what you are explaining, you are "naming" the second character same as the first and only the one character is getting passed in code. Would be helpful to see more of your character instantiation and controller code.

    EDIT: Nevermind, I see your instantiate code, but I won't even try to decipher what this all does. The Networking.xxx code I've never seen and I assume it has to do with Unities built in networking stuff. I use direct RakNet code compiled with SWIG to c#, it gives much lower level access then this. Sorry no clue from this end, hopefully someone else can help =(
     
  8. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    i can upload the project for u? if u want or remote acces with Teamviawer?
     
  9. theBundyXVO

    theBundyXVO

    Joined:
    Sep 15, 2014
    Posts:
    24
    Its a problem with your network character controllers not properly being assigned. Make sure playerIDs are being assigned along with network player controllers to each player.
     
  10. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    can u show me ?
     
  11. theBundyXVO

    theBundyXVO

    Joined:
    Sep 15, 2014
    Posts:
    24
    Its something you can look up, there are a billion tutorials on it. Just google "Unity Network Player Controller Tutorial" or "Unity Network Player Tutorial" and you should have your fix.
     
  12. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    u know Teamviawer? can u check fast?
     
  13. theBundyXVO

    theBundyXVO

    Joined:
    Sep 15, 2014
    Posts:
    24
    I'm spoonfeeding you the answer, I'm not gonna go and code it for you man. Just do your research and fix it with your abilities.
     
    Niekos likes this.