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

Making a server

Discussion in 'Scripting' started by briyan, May 7, 2015.

  1. briyan

    briyan

    Joined:
    Jan 13, 2015
    Posts:
    52
    I was starting to make a server and client program with a login system. But there is something wrong, and i don't know what to do

    NullReferenceException
    UnityEngine.NetworkView.RPC (System.String name, RPCMode mode, System.Object[] args) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/Networking.gen.cs:409)
    Server.OnGUI () (at Assets/Server.cs:78)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public class Server : MonoBehaviour {
    5.     //The ip adress
    6.     public string IP;
    7.     //the port
    8.     public int Port;
    9.     //admin
    10.     public bool admin;
    11.     //first
    12.     public bool First;
    13.     //user
    14.     public bool User;
    15.     //username admin
    16.     private string admin_password;
    17.     //username admin
    18.     private string admin_username;
    19.     //username
    20.     public string username;
    21.     //password
    22.     public string password;
    23.     //list with username's
    24.     public List<string> usernames;
    25.     //list with password's
    26.     public List<string> passwords;
    27.     //list with emails
    28.     public List<string> emails;
    29.     //network view
    30.     public NetworkView networkview;
    31.     // Use this for initialization
    32.     void Start () {
    33.         First = true;
    34.         IP = "HAVEARIGHTIPBUTDONTWANNAPOSTITONTHEFORUM";
    35.         Port = 25001;
    36.         admin_username = "NOTFORYOU";
    37.         admin_password = "NOTFORYOU";
    38.         username = " ";
    39.         password = " ";
    40.         usernames.Add("henk");
    41.         passwords.Add ("henk");
    42.     }
    43.    
    44.     // Update is called once per frame
    45.     void Update () {
    46.    
    47.     }
    48.  
    49.     void OnGUI(){
    50.         if(First == true){
    51.             username = GUI.TextArea(new Rect(0, 0, Screen.width/4, Screen.height/10), username);
    52.             password = GUI.TextArea(new Rect(0, Screen.height/4, Screen.width/4, Screen.height/10), password);
    53.  
    54.             if(GUI.Button(new Rect(0, Screen.height/10 * 8, Screen.width/6, Screen.height/6), "Log in as Admin")){
    55.                 if(username == admin_username && password == admin_password){
    56.                     admin = true;
    57.                     First = false;
    58.                     Network.InitializeServer(10, Port);
    59.                 }
    60.             }  
    61.  
    62.             if(GUI.Button(new Rect(0, Screen.height /10*6, Screen.width/6, Screen.height/6), "Go to User login")){
    63.                 Network.Connect(IP, Port);
    64.                 User = true;
    65.                 First = false;
    66.             }
    67.         }
    68.  
    69.         if(admin == true){
    70.             GUI.Box(new Rect(0, 0, Screen.width, Screen.height), " " + Network.connections.Length);
    71.         }
    72.  
    73.         if(User == true){
    74.             username = GUI.TextArea(new Rect(0, 0, Screen.width/4, Screen.height/4), username);
    75.             password = GUI.TextArea(new Rect(0, Screen.height/4 * 2, Screen.width/4, Screen.height/4), password);
    76.             if(GUI.Button(new Rect(0, Screen.height/10 * 8, Screen.width/6, Screen.height/6), "Log in as Admin")){
    77.                 networkview = new NetworkView();
    78.                 networkview.RPC("Login",RPCMode.Server, "" + username);
    79.             }  
    80.         }
    81.  
    82.  
    83.     }
    84.  
    85.     [RPC]
    86.     void Login(string Username, string Password)
    87.     {
    88.         if(Network.isServer)
    89.         {
    90.             for(int i = 0; i < usernames.Count; i++){
    91.                 if(Username == usernames[i]){
    92.                     if(Password == passwords[i]){
    93.                         networkview.RPC("LoadLevel",RPCMode.Others);
    94.                     }else{
    95.                         Debug.Log("wrong password");
    96.                     }
    97.                 }
    98.             }
    99.            
    100.  
    101.                
    102.         }
    103.     }
    104.  
    105.     [RPC]
    106.     void LoadLevel(){
    107.         if(Network.isClient)
    108.         {
    109.             if(Application.loadedLevel == 0)
    110.             {
    111.                 Application.LoadLevel(1);
    112.             }
    113.         }
    114.     }
    115. }
    116.  
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Your Login method takes 2 arguments but you're only providing 1.

    I also don't think you'll want to be sending people's passwords like that. A Unity server is typically another player hosting a game.
     
    novashot likes this.
  3. briyan

    briyan

    Joined:
    Jan 13, 2015
    Posts:
    52
    Well iam the only one that can start the server (This will be the only server), because this is a launcher, want people to see the news they want about games that are in development.

    But how can i make sure the password will be in this function?
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    and what's to stop something intercepting the packet and stripping out the password... users just lost their account.

    salt and encrypt locally, only send the result to the server.


    just add the password as another parameter on the rpc call by the looks of it, scripting example has 2 parameters: http://docs.unity3d.com/ScriptReference/NetworkView.RPC.html
     
  5. briyan

    briyan

    Joined:
    Jan 13, 2015
    Posts:
    52
    Sorry, but i dont really understand you (Iam just started today with learning the network part of unity3d)
     
  6. briyan

    briyan

    Joined:
    Jan 13, 2015
    Posts:
    52
    i fixed already one thing:)
    and now this one came up

    Can't send RPC function since no connection was started.
    UnityEngine.NetworkView:RPC(String, RPCMode, Object[])
    Server:OnGUI() (at Assets/Server.cs:87)
     
    Random_Civilian likes this.
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    which bit? the handling of passwords or the handling of more parameters across RPC?
     
  8. briyan

    briyan

    Joined:
    Jan 13, 2015
    Posts:
    52
    Yeah fixed it:

    1. i deleted the line before networkview.RPC
     
  9. briyan

    briyan

    Joined:
    Jan 13, 2015
    Posts:
    52
    The only problem is that all the people that opend the program will be logged in if one is logging in