When I try to run
FB.Login("email,publish_actions", LoginCallback)
I get this error:
NullReferenceException: Facebook object is not yet loaded. Did you call FB.Init()? FB.get_FacebookImpl () (at Assets/Facebook/Scripts/FB.cs:23) FB.Login (System.String scope, Facebook.FacebookDelegate callback) (at Assets/Facebook/Scripts/FB.cs:111) Controller.Start () (at Assets/Scripts/Controller.cs:27)
I am running FB.Init (SetInit, OnUnityHide)
and my SetInit and OnUnityHide functions are:
private void SetInit()
{
enabled = true;
}
private void OnUnityHide(bool isGameShown)
{
if(!isGameShown)
{
Time.timeScale = 0;
}
else
{
Time.timeScale = 1;
}
}
Sample code… Do like this
In a fresh new script copy paste this and try deploy it in a device.
private void CallFBInit()
{
FB.Init(OnInitComplete, OnHideUnity);
}
private void OnInitComplete()
{
Debug.Log("FB.Init completed: Is user logged in? " + FB.IsLoggedIn);
}
private void OnHideUnity(bool isGameShown)
{
Debug.Log("Is game showing? " + isGameShown);
}
private void CallFBLogin()
{
FB.Login("email", LoginCallback);
}
private void LoginCallback(FBResult result)
{
if (result.Error != null)
print("Logged In");
else if (!FB.IsLoggedIn) {
print("Failed");
}
}
NOTE TO REMEMBER :
-
Call the CallFBInit() function in your void start.
-
Call the CallFBLogin() function inside a button click. like this
if(GUI.button)
{
CallFBLogin();
}
This will definetly work. but remember that you call CallFBInit() inside start.
My issue was that I was deriving from a script that called FB.Init(), so it was being called twice which apparently un-initialized it.
A good way to debug this is to just write:
void Start()
{
Debug.Log(gameObject);
}
on one of your scripts that calls it. Then of course the one that inherits will also respond once you enter play mode.
However this won’t work if you’re not deriving from, but simply calling FB.Init() somewhere else. In that case you’ll just have to track down said script.
In my case, I turn the Facebook object off at Editor and just turn it on at runtime ([your object name].SetActive(true))and the error message is gone!
also, this is my Facebook initialization code as below:
private void Awake()
{
if (!FB.IsInitialized) {
FB.Init (() => {
if (FB.IsInitialized)
FB.ActivateApp ();
else
Debug.LogError ("Couldn't Initialized Facebook!");
},
isGameShown => {
if (!isGameShown)
Time.timeScale = 0;
else
Time.timeScale = 1;
});
} else {
Debug.LogError ("Facebook Activate App");
FB.ActivateApp ();
}
}
I hope it would be helpful!