Search code examples
iosfacebookfacebook-graph-apifacebook-unity-sdk

FB.Init() Issues on Facebook for Unity SDK 6.0


I use the following codes to initialize Facebook SDK (in OnGUI()):

if(GUI.Button(buttonRect, "Login")) {
  if(!FB.IsInitialized) {
    FB.Init(OnInitComplete, OnHideUnity);
  } else {
    OnInitComplete();
  }
}

And its callback functions:

private void OnInitComplete() {
  Debug.Log("FB.Init completed: Is user logged in? " + FB.IsLoggedIn);
  Application.LoadLevel("NextScreen");
}

private void OnHideUnity(bool isGameShown) {
  Debug.Log("Is game showing? " + isGameShown);
}

Then, I run the App in a real iPhone 5s with iOS 8.1.2, The log shows in Xcode 6:

FB.Init() has already been called. You only need to call this once and only once.

NullReferenceException: Facebook object is not yet loaded.  Did you call FB.Init()?
  at FB.get_FacebookImpl () [0x00000] in <filename unknown>:0 
  at FB.Init (Facebook.InitDelegate onInitComplete, System.String appId, Boolean cookie, Boolean logging, Boolean status, Boolean xfbml, Boolean frictionlessRequests, Facebook.HideUnityDelegate onHideUnity, System.String authResponse) [0x00000] in <filename unknown>:0 
  at FB.Init (Facebook.InitDelegate onInitComplete, Facebook.HideUnityDelegate onHideUnity, System.String authResponse) [0x00000] in <filename unknown>:0 
  at TitleScreen.StartGame () [0x00000] in <filename unknown>:0 
  at TitleScreen.Update () [0x00000] in <filename unknown>:0 

(Filename:  Line: -1)

FB.Init completed: Is user logged in? True

This issue does not happen in Android, or running within Unity. How can I resolve this?

p.s. This error does NOT forbid me from login to Facebook. Everything else is normal.

Referencing other similar questions (this and that), the case is different as the version is different (the Graph API versions used are different too)

Note: Using Unity 4.6.1, Facebook SDK for Unity 6.0.0


Solution

  • I see in this question that someone suggests not relying on FB.IsInitalized because it always returns true.

    I can tell you what worked for me is to call FB.Init in the Awake() function, as done in the Facebook Unity Games Integration Tutorial. I did this in a custom class I created as a singleton, so I know it is only initialized once.

    private void Awake() {
        // Initialize FB SDK              
        enabled = false;                  
        FB.Init(SetInit, OnHideUnity);  
    }
    
    private void SetInit()                                                                       
    {                                                                                            
        Util.Log("SetInit");                                                                  
        enabled = true; // "enabled" is a property inherited from MonoBehaviour                  
        if (FB.IsLoggedIn)                                                                       
        {                                                                                        
            Util.Log("Already logged in");                                                    
            OnLoggedIn();                                                                        
        }                                                                                        
    }