[Solved] Facebook Init confusion

I’m experiencing a bit of confusion getting my first Facebook game off the ground. Everything works as it should when I play the game from the Editor & I’m able to login to Facebook with no problems. When I try it from the FB canvas, however, I receive the following error:

NullReferenceException: Facebook object is not yet loaded. Did you call FB.Init()?

As you can see from the script below, I’m calling FB.Init() in the Start function (I’ve also tried it in Awake, with no difference). I’ve included only a snippet of the script, as the rest is likely not relevant.

void Start()
    {
        // Initialize Facebook
        FB.Init(SetInit, OnHideUnity);
    }
 
    private void SetInit()
    {
        // Debug.Log("Facebook initialization complete!");
        if(FB.IsLoggedIn)
        {
            // Debug.Log("Logged into FB.");
            FBLoggedInMenus(true);
        }
        else
        {
            FBLoggedInMenus(false);
        }
    }

    private void OnHideUnity(bool isGameShown)
    {
        if(!isGameShown)
        {
            Time.timeScale = 0; // pause
        }
        else
        {
            Time.timeScale = 1; // unpause
        }
    }

    public void FBLogin()
    {
        FB.Login("email,publish_actions", AuthCallback);
    }

    void AuthCallback(FBResult result)
    {
        if(FB.IsLoggedIn)
        {
            // Debug.Log("FB login worked!");
            FBLoggedInMenus(true);
        }
        else
        {
            // Debug.Log("FB login failed!");
            FBLoggedInMenus(false);
        }
    }

I’m a bit concerned because, while it’s great that everything works in the Editor, there isn’t much point if that’s the ONLY place it works, haha. Any suggestions would be greatly appreciated.

Should it be relevant, I’m hosting the game on my web server(Godaddy.com) & have an active SSL certificate. The build itself is in Development Mode, and I do have my FB AppID firmly in place in the Facebook → Edit Settings. If there’s any additional information I can provide, please let me know. Thanks!

PS: Thinking that it might be a permissions issue, I’ve also tried setting the FB.Login to just publish actions. Sadly, that didn’t help either.

Hmmm… Further hair tearing and head-desking led me to notice that the FB SDK I’m using, 6.2.2, only supports Unity 5 for mobile… Going to try the v7 beta SDK and see if I get any further that way.

Well, that was a bit of a bust… Now I’m stuck trying to figure out how to deploy to WebGL, which builds fine, but does nothing… I get a loading bar, but once it reaches the end, it just sits there… Can’t find any tutorials on using it, and the docs make it sound oh so simple. double-facepalm

After several days of frustrating experimentation, I have at last managed to get things to work as expected. Turns out it was a combination of browser security settings (allowing popups from FB and my own website) and FB app settings. Oddly, even though FB says that you should turn Unity Integration OFF if you’re using the new 7+ versions of the SDK, it wouldn’t work for me at all until I decided to turn it back ON. Now everything works as it should, and I’m finally back in business! :slight_smile:

2 Likes

Can you please explain this.

please explicamos :frowning: