Search Unity

Object reference not set to an instance of an object

Discussion in 'Getting Started' started by Parabornn, Dec 11, 2016.

  1. Parabornn

    Parabornn

    Joined:
    Dec 11, 2016
    Posts:
    1
    Hey guys, I cannot figure out why this error is occurring, I've looked at other people who have reported the same error but it appears to be completely unrelated. Any help is appreciated! =) Below are the 2 files where the problem occurs, Unity says it's on this line: goldDisplay.text = "Gold: " + CurrencyConverter.Instance.GetCurrencyIntoString(gold);

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Click : MonoBehaviour {
    6.  
    7.     public UnityEngine.UI.Text gpc;
    8.     public UnityEngine.UI.Text goldDisplay;
    9.     public float gold = 0;
    10.     public int goldPerClick = 1;
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.         goldDisplay.text = "Gold: " + CurrencyConverter.Instance.GetCurrencyIntoString(gold);
    15.         gpc.text = goldPerClick + " gold/click";
    16.     }
    17.  
    18.     public void Clicked () {
    19.         gold += goldPerClick;
    20.  
    21.     }
    22. }
    23.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CurrencyConverter : MonoBehaviour {
    6.  
    7.     private static CurrencyConverter instance;
    8.     public static CurrencyConverter Instance{
    9.         get{
    10.             return instance;
    11.         }
    12.     }
    13.  
    14.     void Awake(){
    15.         CreateInstance ();
    16.     }
    17.  
    18.     void CreateInstance(){
    19.         if (instance == null) {
    20.             instance = this;
    21.         }
    22.     }
    23.  
    24.     public string GetCurrencyIntoString(float valueToConvert) {
    25.         string[] suffixes = {"k", "m", "b", "t", "q"};
    26.         int place = 0;
    27.         if (valueToConvert >= 1000) {
    28.             while (valueToConvert >= 1000) {
    29.                 valueToConvert /= 1000;
    30.                 place++;
    31.             }
    32.         } else {
    33.             return "" + valueToConvert;
    34.         }
    35.         return (valueToConvert).ToString ("f3") + suffixes [place - 1];
    36.     }
    37. }
    38.