• 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

· Add your 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.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Why isn't Playgameservices authenticating 0 Answers

How to Implement Google Play Games? 0 Answers

Google Play services authentication issue 0 Answers

Google Play leaderboard won't show - "Hmm, something went wrong in play games" 0 Answers

Can sign in using Google Play Games Plugin, but unable to load scores from the leaderboard 2 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