0
\$\begingroup\$

I am trying to connect fb with my game. its first time i am connecting fb with game. i downloaded fb SDk 7.3.0 and i am using unity 5.2.1f1. i imported the sdk in my game asset. then i created a new c# script "FBholder". in Awake() i tried to use FB.Init(). but its not accepting. all other scripts used inside the game are calling correctly, only the scripts came with fb sdk is not working.

 using UnityEngine;
 using System.Collections;

public class Fbholder : MonoBehaviour {

// Use this for initialization
void Awake() {
    FB.Init (SetInit, OnHideUnity);// This line is not accepting
}
}

i Used this Video for my referance

\$\endgroup\$
4
  • \$\begingroup\$ any error or logs? \$\endgroup\$ Jan 11, 2016 at 6:27
  • \$\begingroup\$ yes, The name `FB' does not exist in the current context \$\endgroup\$ Jan 11, 2016 at 6:28
  • \$\begingroup\$ but it exist in my project, If i tried to create another c# script as Fb means it displays an error message as " The nameSpace already exist' \$\endgroup\$ Jan 11, 2016 at 6:30
  • \$\begingroup\$ check the edits \$\endgroup\$ Jan 11, 2016 at 6:33

1 Answer 1

3
\$\begingroup\$

You have not included the correct namespace for FB.

Write using Facebook.Unity; at top of your class.

Here is the sample script for using Facebook with common features, such as posting score, with latest SDK.

using Facebook.Unity;

List<string> perms = new List<string> (){"public_profile", "email", "user_friends"};

void Awake ()
    {
        if (!FB.IsInitialized) {
            // Initialize the Facebook SDK
            FB.Init (InitCallback, OnHideUnity);
        } else {
            // Already initialized, signal an app activation App Event
            FB.ActivateApp ();
        }
    }

private void InitCallback ()
    {
        if (FB.IsInitialized) {
            // Signal an app activation App Event
            FB.ActivateApp ();
            // Continue with Facebook SDK
            // ...
        } else {
            Debug.Log ("Failed to Initialize the Facebook SDK");
        }
    }

    private void OnHideUnity (bool isGameShown)
    {
        if (!isGameShown) {
            // Pause the game - we will need to hide
            Time.timeScale = 0;
        } else {
            // Resume the game - we're getting focus again
            Time.timeScale = 1;
        }
    }

    private void AuthCallback (ILoginResult result)
    {
        if (FB.IsLoggedIn) {
            // AccessToken class will have session details
            var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
            // Print current access token's User ID
            Debug.Log (aToken.UserId);
            // Print current access token's granted permissions
            foreach (string perm in aToken.Permissions) {
                Debug.Log (perm);
            }
        } else {
            Debug.Log ("User cancelled login");
        }
    }

// On Facebook login button
public void OnFacebook ()
    {
        FB.LogInWithReadPermissions (perms, AuthCallback);
    }
\$\endgroup\$
8
  • \$\begingroup\$ yap, its working, but i have another doubt. in my reference video he's also not used using Facebook.Unity;. their its worked correctly. here its not working. why? \$\endgroup\$ Jan 11, 2016 at 6:34
  • \$\begingroup\$ May be he was using older sdk \$\endgroup\$ Jan 11, 2016 at 6:35
  • \$\begingroup\$ i used this scripting, its not asking access token. it displays a message in console "This only needs to be called for iOS or Android" and "Using Facebook Unity SDK v7.3.0 with FBUnityEditorSDK/7.3.0" \$\endgroup\$ Jan 11, 2016 at 9:19
  • \$\begingroup\$ Ignore this message \$\endgroup\$ Jan 11, 2016 at 9:42
  • \$\begingroup\$ its not asking the access token \$\endgroup\$ Jan 11, 2016 at 9:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .