Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Facebook Login NullReference Error

Discussion in 'Immediate Mode GUI (IMGUI)' started by kjkjkjhan, Oct 10, 2017.

  1. kjkjkjhan

    kjkjkjhan

    Joined:
    Jun 24, 2017
    Posts:
    3
    I get following error when I try to log in using Facebook SDK

    NullReferenceException: Object reference not set to an instance of an object
    UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, Int32 instanceID, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style) (at C:/buildslave/unity/build/Runtime/IMGUI/Managed/GUI.cs:1836)

    The Error Occurs after FBLogin() is called. When I click sendSucess or sendError, it spams the above Error. Any Idea?

    Code (CSharp):
    1. public class FBScript : MonoBehaviour {
    2.  
    3.     public GameObject StateTransition;
    4.  
    5.     private void Awake()
    6.     {
    7.         if (!FB.IsInitialized)
    8.         {
    9.             FB.Init(SetInit, OnHideUnity);
    10.         }
    11.         else
    12.         {
    13.             FB.ActivateApp();
    14.         }
    15.     }
    16.  
    17.     private void SetInit()
    18.     {
    19.         if (FB.IsInitialized)
    20.         {
    21.             FB.ActivateApp();
    22.         }
    23.         else
    24.         {
    25.             Debug.Log("Failed to Initialize the Facebook SDK");
    26.         }
    27.     }
    28.  
    29.     private void OnHideUnity(bool isGameShown)
    30.     {
    31.         if (!isGameShown)
    32.         {
    33.             // pause the game
    34.             Time.timeScale = 0;
    35.         }
    36.         else
    37.         {
    38.             // resume the game
    39.             Time.timeScale = 1;
    40.         }
    41.     }
    42.  
    43.     public void FBLogin()
    44.     {
    45.         List<string> permissions = new List<string>() { "public_profile", "email", "user_friends" };
    46.         FB.LogInWithReadPermissions(permissions, AuthCallBack);
    47.     }
    48.  
    49.     private void AuthCallBack(IResult result)
    50.     {
    51.         if (result.Error != null)
    52.         {
    53.             Debug.Log(result.Error);
    54.         }
    55.         else
    56.         {
    57.             if (FB.IsLoggedIn)
    58.             {
    59.                 Debug.Log("Is Logged In");
    60.                 var aToken = AccessToken.CurrentAccessToken;
    61.                 Debug.Log(aToken.UserId);
    62.  
    63.                 FetchFBProfile();
    64.                 SwitchScreen(FB.IsLoggedIn);
    65.             }
    66.             else
    67.                 Debug.Log("Is Not Logged In");
    68.         }
    69.     }
    70.  
    71.     private void FetchFBProfile()
    72.     {
    73.         FB.API("/me?fields=first_name,last_name,email,friends", HttpMethod.GET, FetchProfileCallback, new Dictionary<string, string>() { });
    74.     }
    75.  
    76.     private void FetchProfileCallback(IGraphResult result)
    77.     {
    78.  
    79.         Debug.Log(result.RawResult);
    80.  
    81.         IDictionary<string, object> dict = (Dictionary<string, object>)result.ResultDictionary;
    82.  
    83.         Debug.Log("Profile: first name: " + dict["first_name"]);
    84.         Debug.Log("Profile: last name: " + dict["last_name"]);
    85.         Debug.Log("Profile: id: " + dict["id"]);
    86.         Debug.Log("Profile: email: " + dict["email"]);
    87.  
    88.         string username = dict["email"].ToString();
    89.         string password = dict["id"].ToString();
    90.     }
    91.  
    92.     public void LogOut()
    93.     {
    94.         if (FB.IsLoggedIn)
    95.         {
    96.             FB.LogOut();
    97.         }
    98.     }
    99.  
    100.     private void SwitchScreen(bool isLoggedIn)
    101.     {
    102.         if (isLoggedIn)
    103.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    104.         else
    105.             StateTransition.GetComponent<SwitchViewScript>().StartCanvasOn();
    106.     }
    I am using latest Unity version (2017.1.1f1) and FB SDK.(7.10.0)