Page 1 of 1

Noobie Question - C# login script

Posted: 21 May 2009, 00:00
by squirrel
Hello - I've been using Unity for a while but using Jscript and am unfamiliar with C# (learning though!) Anyway, I have relatively simple script that connects to SFS and then tries to pass a login, but I'm getting a null reference on the smartFox.Login.

Code:

Code: Select all

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using SmartFoxClientAPI;
using SmartFoxClientAPI.Data;

public class NewConnect : MonoBehaviour {
    private SmartFoxClient smartFox;
    private string statusMessage = "";
    private string zone = "Sorrow";
    private string username = "";
    private string password = "";
    private string loginErrorMessage = "";
    private bool isLoggedIn;
    public GUISkin gSkin;

   // Use this for initialization
   void Start () {
        SmartFoxClient smartFox = new SmartFoxClient();
        smartFox.Connect("*.*.*.*", 9339);
        //Resgister Callbacks
        SFSEvent.onConnection += OnConnection;
        SFSEvent.onLogin += OnLogin;
        SFSEvent.onLogout += OnLogout;
   }
   
   // Update is called once per frame
   void Update () {
   
   }
   
    //Do the connection
    void OnConnection(bool success, string error)
    {
        if (success)
        {
            SmartFox.Connection = smartFox;
            statusMessage = "Connected!";
            Debug.Log("OnConnection Worked");
        }
        else
        {
            statusMessage = "Can't connect!";
        }
    }
    //Login Callback
    void OnLogin(bool success, string name, string error)
    {
        Debug.Log("On Login callback got: " + success + " : " + error + " : " + name);

        if (success)
        {
            isLoggedIn = true;
        }
        else
        {
            loginErrorMessage = error;
            Debug.Log("Login error: " + error);
        }
    }
    //Logout Callback
    void OnLogout()
    {
        Debug.Log("OnLogout");
        isLoggedIn = false;
    }
   
    void OnGUI()
    {
        GUI.skin = gSkin;

        GUI.Label(new Rect(10, 10, 500, 100), "Status: " + statusMessage);

        // Login
        if (!isLoggedIn)
        {
            GUI.Label(new Rect(10, 90, 100, 100), "Zone: ");
            zone = GUI.TextField(new Rect(100, 90, 200, 20), zone, 25);

            GUI.Label(new Rect(10, 116, 100, 100), "Userame: ");
            username = GUI.TextField(new Rect(100, 116, 200, 20), username, 25);

            GUI.Label(new Rect(10, 142, 100, 100), "Password: ");
            password = GUI.TextField(new Rect(100, 142, 200, 20), password, 4);

            GUI.Label(new Rect(10, 218, 100, 100), loginErrorMessage);

            if (GUI.Button(new Rect(100, 166, 100, 24), "Login") || (Event.current.type == EventType.keyDown && Event.current.character == '\n'))
            {
****ERROR ON THIS LINE    smartFox.Login(zone, username, password); ***
             }
        }
        else
        {
            // If they're there
            if (GUI.Button(new Rect(580, 478, 90, 24), "Logout"))
            {
                smartFox.Logout();
            }
        }
    }
}


I'm sure it's something simple and I'll be embarrassed but I can't figure it out. I have other scripts that work - and I must be blind because I can't see the difference, and the console in Unity is not throwing any Debug.Log errors. Any thoughts appreciated.

Posted: 21 May 2009, 07:35
by ThomasLund
Ahh yes - its one of the easy fixes

Code: Select all

void Start () {
        SmartFoxClient smartFox = new SmartFoxClient();


You are here overriding the class variable with a local variable. Remove the SmartFoxClient first part and I think it should work.

Posted: 21 May 2009, 13:49
by squirrel
ThomasLund wrote:Ahh yes - its one of the easy fixes

Code: Select all

void Start () {
        SmartFoxClient smartFox = new SmartFoxClient();


You are here overriding the class variable with a local variable. Remove the SmartFoxClient first part and I think it should work.


Oh dear lord. Thank you, amazing I couldn't see that. Works fine now, as it should!

Thanks!