- Home /
How to access my highscore from another scene ?
i want to access my highscore from another script and another scene to my current scene so that i can make a scoreboard link with facebook. But the problem is that i always get my highscore as zero. This is my facebook script ,(check "public void SetScore")
'using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class FBholder : MonoBehaviour {
public GameObject UIFBIsLoggedIn;
public GameObject UIFBNotLoggedIn;
public GameObject UIFBAvatar;
public GameObject UIFBUserName;
private List<object> scoresList = null;
public GameObject ScoreEntryPanel;
public GameObject ScoreScrollList;
private Dictionary<string, string> profile = null;
void Awake()
{
FB.Init (SetInit, OnHideUnity);
}
private void SetInit()
{
Debug.Log ("FB Init done.");
if(FB.IsLoggedIn)
{
DealWithFBMenus(true);
Debug.Log ("FB Logged In");
}else{
DealWithFBMenus(false);
}
}
private void OnHideUnity(bool isGameShown)
{
if(!isGameShown)
{
Time.timeScale = 0;
}else{
Time.timeScale = 1;
}
}
public void FBlogin()
{
FB.Login ("email,publish_actions", AuthCallback);
}
void AuthCallback(FBResult result)
{
if(FB.IsLoggedIn){
Debug.Log ("FB login worked!");
DealWithFBMenus(true);
}else{
Debug.Log ("FB Login fail");
DealWithFBMenus(false);
}
}
void DealWithFBMenus(bool isLoggedIn)
{
if(isLoggedIn){
UIFBIsLoggedIn.SetActive (true);
UIFBNotLoggedIn.SetActive(false);
// get profile picture code
FB.API (Util.GetPictureURL("me", 128, 128), Facebook.HttpMethod.GET, DealWithProfilePicture);
FB.API ("/me?fields=id,first_name", Facebook.HttpMethod.GET, DealWithUserName);
// get username code
}else{
UIFBIsLoggedIn.SetActive (false);
UIFBNotLoggedIn.SetActive(true);
}
}
void DealWithProfilePicture(FBResult result)
{
if(result.Error != null)
{
Debug.Log ("problem with getting profile picture");
FB.API (Util.GetPictureURL("me", 128, 128), Facebook.HttpMethod.GET, DealWithProfilePicture);
return;
}
Image UserAvatar = UIFBAvatar.GetComponent<Image>();
UserAvatar.sprite = Sprite.Create (result.Texture, new Rect(0,0,128,128), new Vector2(0,0));
}
void DealWithUserName(FBResult result)
{
if(result.Error != null)
{
Debug.Log ("problem with getting profile picture");
FB.API ( "/me?fields=id,first_name", Facebook.HttpMethod.GET, DealWithUserName);
return;
}
profile = Util.DeserializeJSONProfile(result.Text);
Text UserMsg = UIFBUserName.GetComponent<Text>();
UserMsg.text = "Welcome, " + profile["first_name"];
}
public void ShareWithFriends()
{
FB.Feed (
linkCaption: "I'm playing this awesome game",
picture: "http://i58.tinypic.com/vqhdts.png",
linkName: "Check out this new game",
link: "http://apps.facebook.com/" + FB.AppId + "/?challenge_brag=" + (FB.IsLoggedIn ? FB.UserId : "guest")
);
}
public void InviteFriends()
{
FB.AppRequest(
message: "I challenge you to beat my score.",
title: "Challenge your friends. "
);
}
// All Scores API related Things
public void QueryScores()
{
FB.API ("/app/scores?fields=score,user.limit(30)", Facebook.HttpMethod.GET, ScoresCallback);
}
private void ScoresCallback(FBResult result)
{
Debug.Log ("Scores callback: " + result.Text);
scoresList = Util.DeserializeScores (result.Text);
foreach (Transform child in ScoreScrollList.transform)
{
GameObject.Destroy (child.gameObject);
}
foreach (object score in scoresList)
{
var entry = (Dictionary<string,object>) score;
var user = (Dictionary<string,object>) entry["user"];
GameObject ScorePanel;
ScorePanel = Instantiate (ScoreEntryPanel) as GameObject;
ScorePanel.transform.parent = ScoreScrollList.transform;
Transform ThisScoreName = ScorePanel.transform.Find ("FriendName");
Transform ThisScoreScore = ScorePanel.transform.Find ("FriendScore");
Text ScoreName = ThisScoreName.GetComponent<Text>();
Text ScoreScore = ThisScoreScore.GetComponent<Text>();
ScoreName.text = user["name"].ToString();
ScoreScore.text = entry["score"].ToString();
Transform TheUserAvatar = ScorePanel.transform.Find ("FriendAvatar");
Image UserAvatar = TheUserAvatar.GetComponent<Image>();
FB.API (Util.GetPictureURL(user["id"].ToString (), 128,128), Facebook.HttpMethod.GET, delegate(FBResult pictureResult){
if(pictureResult.Error != null) // if there was an error
{
Debug.Log (pictureResult.Error);
}
else // if everything was fine
{
UserAvatar.sprite = Sprite.Create (pictureResult.Texture, new Rect(0,0,128,128), new Vector2(0,0));
}
});
}
}
**public void SetScore()
{
GameObject High = GameObject.FindGameObjectWithTag("Score");
ScoreController scoreController = High.GetComponent <ScoreController>();
int high = scoreController.HighScore;
var scoreData = new Dictionary<string,string> ();
scoreData ["score"] = high.ToString();
FB.API ("/me/scores", Facebook.HttpMethod.POST, delegate(FBResult result) {
Debug.Log ("Score submit result: " + result.Text);
}, scoreData);
}**
}`
And this is my scoreController script in another scene
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScoreController : MonoBehaviour {
public Text ScoreText;
public int score;
private int Count = 2;
public Text TotalScoreText;
public Text HighScoreText;
public int HighScore;
void Start ()
{
score = -2;
UpdateScore ();
AddScore ();
}
public void UpdateScore()
{
ScoreText.text = "Score: " + score;
TotalScoreText.text = "TOTAL SCORE -: " + score;
HighScoreText.text = "HIGH SCORE -: " + HighScore;
}
public void AddScore( )
{
score += Count;
if(score>PlayerPrefs.GetInt ("HIGHSCORE_KEY",0))
{
PlayerPrefs.SetInt ("HIGHSCORE_KEY",score);
}
HighScore = PlayerPrefs.GetInt ("HIGHSCORE_KEY",0);
UpdateScore ();
}
}
Comment
Answer by ZenRumiko · Aug 15, 2015 at 11:02 PM
DontDestroyOnLoad();
will stop a gameobject from being destroyed during a scene transition. You could also use a static variable.