Well I try to do some game in Unity2D, and in this game I put the Facebook SDK, and when I try to share de punctuation of my game, I have two this error:
- NullReferenceException: Object reference not set to an instance of an object
- Getting control 1’s position in a group with only 1 controls when doing Repaint
And I don’t know how to fix it.
Anybody can help me ?
My codi is this:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Facebook.MiniJSON;
public sealed class facebookControl : MonoBehaviour {
private bool isInit = false;
private int highScore = 0;
static facebookControl instance = null;
public string FullName;
public string Gender;
public GUISkin MenuSkin;
public Rect LoginButtonRect; // Position of login button
void Start()
{
if (!FB.IsLoggedIn)
{
CallFBLogin();
}
}
void Awake ()
{
if (instance == null)
instance = this;
enabled = false;
CallFBInit(); //Inicializamos el SDK de Facebook
//Cojemos la puntiacion mas alta
highScore = PlayerPrefs.GetInt("High Score", 0);
DontDestroyOnLoad(gameObject);
}
private void CallFBInit()
{
FB.Init(SetInit, OnHideUnity);
}
private void SetInit()
{
Debug.Log("SetInit");
enabled = true; // "enabled" is a property inherited from MonoBehaviour
if (FB.IsLoggedIn)
{
Debug.Log("Already logged in");
OnLoggedIn();
}
isInit = true;
}
private void OnHideUnity(bool isGameShown)
{
Debug.Log("Is game showing? " + isGameShown);
}
public static facebookControl Instance ()
{
return instance;
}
public string ApiQuery = "";
private string lastResponse = "";
private Texture2D lastResponseTexture;
void OnGUI() {
//GUI.skin = MenuSkin;
//GUILayout.Box("", MenuSkin.GetStyle("box"));
Debug.Log(FB.AppId);
Debug.Log(FB.UserId);
GUI.enabled = isInit;
}
#region FB.Login() example
private void CallFBLogin()
{
FB.Login("email,publish_actions", LoginCallback);
}
void LoginCallback(FBResult result)
{
if (result.Error != null)
lastResponse = "Error Response:\n" + result.Error;
else if (!FB.IsLoggedIn)
{
lastResponse = "Login cancelled by Player";
}
else
{
lastResponse = "Login was successful!";
FB.API("/me?fields=id,name", Facebook.HttpMethod.GET, APICallback);
sharePunctuation();
}
Debug.Log (lastResponse);
}
void APICallback(FBResult result)
{
result_str = result.Text;
profile = Util.DeserializeJSONProfile(result.Text);
}
private void CallFBLogout()
{
FB.Logout();
}
#endregion
/*Sirve para compartir la puntuacion en Facebook*/
private void sharePunctuation()
{
Debug.Log("sharePunctuation");
FB.Feed(
linkCaption: "Acabo de hacer mi record " + highScore,
picture: "http://www.friendsmash.com/images/logo_large.jpg",
linkName: "Mole miner",
link: "http://apps.facebook.com/" + FB.AppId + "/?challenge_brag=" + (FB.IsLoggedIn ? FB.UserId : "guest")
);
}
void OnLoggedIn()
{
Debug.Log("Logged in. ID: " + FB.UserId);
}
void Callback(FBResult result)
{
lastResponseTexture = null;
// Some platforms return the empty string instead of null.
if (!String.IsNullOrEmpty(result.Error))
lastResponse = "Error Response:\n" + result.Error;
else if (!ApiQuery.Contains("/picture"))
lastResponse = "Success Response:\n" + result.Text;
else
{
lastResponseTexture = result.Texture;
lastResponse = "Success Response:\n";
}
}
#region FB.AppRequest() Direct Request
public string DirectRequestTitle = "";
public string DirectRequestMessage = "Herp";
private string DirectRequestTo = "";
private void CallAppRequestAsDirectRequest()
{
if (DirectRequestTo == "")
{
throw new ArgumentException("\"To Comma Ids\" must be specificed", "to");
}
FB.AppRequest(
DirectRequestMessage,
DirectRequestTo.Split(','),
"",
null,
null,
"",
DirectRequestTitle,
Callback
);
}
#endregion
/*#region FB.AppRequest() Friend Selector
public string FriendSelectorTitle = "";
public string FriendSelectorMessage = "Derp";
public string FriendSelectorFilters = "[\"all\",\"app_users\",\"app_non_users\"]";
public string FriendSelectorData = "{}";
public string FriendSelectorExcludeIds = "";
public string FriendSelectorMax = "";
private void CallAppRequestAsFriendSelector()
{
// If there's a Max Recipients specified, include it
int? maxRecipients = null;
if (FriendSelectorMax != "")
{
try
{
maxRecipients = Int32.Parse(FriendSelectorMax);
}
catch (Exception e)
{
//status = e.Message;
}
}
// include the exclude ids
string[] excludeIds = (FriendSelectorExcludeIds == "") ? null : FriendSelectorExcludeIds.Split(',');
FB.AppRequest(
FriendSelectorMessage,
null,
FriendSelectorFilters,
excludeIds,
maxRecipients,
FriendSelectorData,
FriendSelectorTitle,
Callback
);
}
#endregion*/
}