Search code examples
facebookunity-game-enginefacebook-unity-sdk

Facebook unity SDK, "FB.Init() has already been called"


I am working with FB Unity SDK, and one constant problem i am facing is to check if FB.Init() has already been called.

Scenario:

  1. There is a fb connect button, user clicks it.
  2. If the user decides to cancel the dialog and click "Fb connect again"

FB sdk throws a notice saying "FB.Init() has already been called. You need to call this only once."

Here is what i am trying to do, but does'nt work

    if(FB.AccessToken=="" || FB.AccessToken==null){
        Debug.Log ("Fb not init(), call it");
        FB.Init(OnInitComplete, null);
    }else{
        Debug.Log ("Facebook already init()");
        OnInitComplete();           
    }

But, obviously this is not working since FB.Init() was called and the user cancelled it. How can i verify if FB.Init() has already been called? But i was expecting accessToken to be null?

And, do these messages effect the submission of the app? Will they be displayed in the production build?


Solution

  • FB.Init() shouldn't be called more than once. This is by design (because it inits Facebook GameObjects and such within Unity). Instead, do this:

    FB.Init(MyOnInitComplete);
    
    ....
    
    public void MyOnInitComplete() {
        // FB.IsLoggedIn checks for the FB.AccessToken and the FB.UserId
        if(!FB.IsLoggedIn){
            // FB.Init() is called, but user is still not logged in.
            FB.Login("<your_permissions_here>");
        } else {
            // User is logged in & FB.Init is called       
        }
    }