• 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
Question by PaoloG79 · Nov 27, 2019 at 06:36 PM · nullreferenceexceptiongoogle play gamesleaderboard

Problem implementing Leaderboard button

Hello. I want to add a leaderboard to my android game. I've created one in google play console successfully. I've used three scripts for managing score, leaderbooards and game/player:

  1. LeaderBoardManagerScript where I manage everything related to leaderboards (login, add score and show leaderboard.

  2. ScoreScript, where I manage everything related to score system

  3. Player script, where I manage player and game behavior.

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     using GooglePlayGames;
     using UnityEngine.SocialPlatforms;
     using UnityEngine.UI;
     
     public class LeaderboardManagerScript : MonoBehaviour
     {
         public int score;
         public static LeaderboardManagerScript current;
         
         private void Awake()
         {
             if (current == null)
                 current = this;
         }
     
         // Start is called before the first frame update
         void Start()
         {
             PlayGamesPlatform.Activate();
             Login();
         }
     
         // Update is called once per frame
         void Update()
         { 
             
         }
     
         public void Login()
         {
             Social.localUser.Authenticate((bool success) => 
             { 
             });
         }
     
         public void AddScore()
         {
             Social.ReportScore(ScoreScript.current.score, Leaderboard.leaderboard_best_scores, (bool success) =>
             {
             });
         }
     
         public void ShowLeaderboard()
         {
             if (Social.localUser.authenticated)
             {
                 PlayGamesPlatform.Instance.ShowLeaderboardUI(Leaderboard.leaderboard_best_scores);
             }
             else
             {
                 Login();
             }
         }
     }
     
    
     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     using UnityEngine.UI;
     
     public class ScoreScript : MonoBehaviour
     {
         public int score;
         public Text scoreText;
         public Text highScoreText;
     
         public static ScoreScript current;
     
         private void Awake()
         {
             if (current == null)
                 current = this;
         }
     
         void Start()
         {
             score = 0;
         }
     
         void Update()
         {
             scoreText.text = score.ToString();
         }
     
         private void OnTriggerEnter(Collider other)
         {
             if (other.gameObject.tag == "Score")
             {
                 score++;
                     PlayerScript.current.playerSpeed = PlayerScript.current.playerSpeed + 3;
             }
                 
     
             if (other.gameObject.tag == "Score2")
             {
                 score += 2;
                     PlayerScript.current.playerSpeed = PlayerScript.current.playerSpeed + 3;
             }
     
             if (other.gameObject.tag == "Jackpot") 
             { 
                 score += 5;
                     PlayerScript.current.playerSpeed = PlayerScript.current.playerSpeed + 5;
             }
         }  
     
         public void CheckHighScore()
         {
             if (PlayerPrefs.HasKey("highScore"))
             {
                 if (score > PlayerPrefs.GetInt("highScore"))
                     PlayerPrefs.SetInt("highScore", score); 
             }
             else
                 PlayerPrefs.SetInt("highScore", score); 
     
             highScoreText.text = PlayerPrefs.GetInt("highScore").ToString(); 
         }
     }
    
    

I've created a button in canvas for leaderboard, set the onclick event to playerScript where I put the following void

     public void ShowLeaderboard()
     {
         LeaderboardManagerScript.current.ShowLeaderboard();
     }


when I run the game, if I push the Leaderboard button, I get a null reference exception. Not sure why, so I'm asking help...

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by KittenSnipes · Nov 29, 2019 at 09:04 AM

@PaoloG79

The problem might be how you get the reference for the leaderboardmanagerscript object. I’m guessing that you either never get a reference of said script attached to a game object or at some point when you are getting it that it is set to null. You should show how you get said reference if you get it at all. If you never get the reference then check where the script is attached and get that reference. Best of luck.

Comment
PaoloG79

People who like this

1 Show 1 · 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
avatar image PaoloG79 · Nov 29, 2019 at 03:27 PM 0
Share

Thanks for your answer. I actually managed to fix the error, script was attached to the wrong game Object. Me dumb.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

123 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there an easy way to tell if Social.ReportScore is passed a new highscore? 0 Answers

Loading Scores from Google Leaderboard always shows 1 Entry in a Leaderboard with over 5 scores posted 0 Answers

Google Play Games doesn't show signing popup on mobile 0 Answers

Google Play Games Plugin causing errors on import 3 Answers

Google play Service Leaderboard problem 0 Answers


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