title: How to fix NullReferenceExeption: Object reference?
body: <p>I am new to game dev and this is my first time to develop a game. Here I am trying to print the total score in all game collected by the player. Like if the player played for 10 times then the score 1 to 10 is added to the total coin. In this code, I want to show it to the UI.</p>
  <p><a href="https://i.stack.imgur.com/4vbpK.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>Here is the error that I recieved: <code>NullReferenceException: Object reference not set to an instance of an object MainManager.Start () (at Assets/Scripts/Main/MainManager.cs:28)</code></p>
  <p>As you can see here in my code, this line <code>highscore.text = &quot;&quot; + PlayerPrefs.GetInt(&quot;bestScore&quot;);</code> has no error. But in this line <code>totalcoin.text = &quot;&quot; + PlayerPrefs.GetInt(&quot;totalCoin&quot;);</code> the error of object reference persist.</p>
  <p>Here is the full code:</p>
  <pre><code>using System.Net.Mime;
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
  using UnityEngine.SceneManagement;
  
  public class MainManager : MonoBehaviour {
  
      public static MainManager instance;
  
      public Text highscore;
      public Text totalcoin;
  
      GameCounter counter;
  
  
      void Awake() {
          Time.timeScale = 1f;
  
          if (instance == null) {
              instance = this;
          }
      }
  
      void Start () {
          highscore.text = &quot;&quot; + PlayerPrefs.GetInt(&quot;bestScore&quot;);// This already show the best score for every game
          totalcoin.text = &quot;&quot; + PlayerPrefs.GetInt(&quot;totalCoin&quot;); // Here is the error pointing . Not showing the total coin for all the games the player played
           if ((counter == null) &amp;&amp; (GetComponent&lt;GameCounter&gt;() != null))
           {
               counter = GetComponent&lt;GameCounter&gt;();
           }
           else
           {
               Debug.LogWarning(&quot;...&quot;);
           }
      }
      
      void Update () {
          
      }
  
      public void PlayGame() {
          counter.PlayGameLimit();
      }
  
      public void QuitGame() {
          Application.Quit();
      }
  }
  </code></pre>