Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Abhi94 · Aug 15, 2015 at 05:08 PM · script.facebookhighscore

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Score script not working , Wanting to show high score but can't. please help !! 2 Answers

Post highscore to Facebook 0 Answers

whats wrong with my high score code? 1 Answer

I'm trying to set a script to inactive 1 Answer

Implement own highscore script into existing FB SDK example 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges