Click here to Skip to main content
15,664,808 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please read before this question is reported / taken offline! This question has already been taken offline on StackOverflow and I was pointed to the resolution of a general NullReferenceException. But these solutions somehow don't work for me 😥. Also, I'm a beginner 😅.

I get the well-known 'NullReferenceException: Object reference not set to an instance of an object' error. The detailed description: 'Coin.OnTriggerEnter2D (UnityEngine.Collider2D col) (at Assets/Space Adventure/Scripts/Game/Coin.cs:34)'.

These are my two scripts:

Coin Script (where I try to execute the SendBitcoin):

using UnityEngine;
public class Coin : MonoBehaviour
    private Animation anim;
    private AudioSource audioSource;
    private bool taken;
    void Start()
        anim = this.GetComponent<Animation>();
        audioSource = this.GetComponent<AudioSource>();
        taken = false;
    void OnTriggerEnter2D(Collider2D col)
        // Checks if coin has been taken. (Because player can re-enter coin while coin destroy animation is playing)
            // Checks if coin collides with one of the player colliders.
            if( == "Front" || == "Back")
                // Increases player wallet amount.
                Wallet.SetAmount(Wallet.GetAmount() + 1);
                // Play coin destroy animation.
                // Play coin collect sound.
                // Sets coin as taken.
                taken = true;
                // SEND BITCOIN **(here is line 34 and apparently the error)**

Bitcoin Controller (where the Bitcoin should be send):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class GamerTagSend {
    public string gamertag;
    public string amount;
    public string description;
public class BitcoinController : MonoBehaviour
    public static BitcoinController Instance;
    private void Awake()
        Instance = this;
    public void SendBitcoin()
    IEnumerator SendBitcoinCont()
        UnityWebRequest request = new UnityWebRequest("[here is the url]", "POST");
        GamerTagSend postData = new GamerTagSend();
        postData.gamertag = "gamertag";
        postData.amount = "1";
        postData.description = "It works : )";
        string myjson = JsonUtility.ToJson(postData);
        byte[] postDataBytes = System.Text.Encoding.UTF8.GetBytes(myjson);
        request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
        request.uploadHandler = (UploadHandlerRaw)new UploadHandlerRaw(postDataBytes);
        request.SetRequestHeader("Content-Type", "application/json");
        request.SetRequestHeader("apikey", "[here is my api key]");
        yield return request.SendWebRequest();
        if (request.isNetworkError || request.isHttpError)
            Debug.LogError("Error " + request.downloadHandler.text);

I hope you can help me. Thanks in advance. I've been trying to solve this problem for ages. I don't get it.

Please suggest code changes.

What I have tried:

Asking on StackOverflow (removed), searching the web, and searching the official Unity documentation.
Updated 24-Apr-22 4:21am
Richard MacCutchan 23-Apr-22 12:18pm    
The message is telling you that either BitcoinController or Instance is null. You will need to do some debugging to find out why.
pay pal 2022 23-Apr-22 13:28pm    
Thank you! Instance should be 'this'. (set in the awake function of the BitcoinController MonoBehaviour). How do I set BitcoinController to something? Isnt that set in 'public static BitcoinController Instance'? Or am I completely wrong?
Richard MacCutchan 24-Apr-22 3:19am    
Yes, it should be, but is it? As I suggested above, you need to make use of the debugger.
pay pal 2022 24-Apr-22 4:13am    
Solved it by making the gameobject active 😅

1 solution

Answered only to remove from unanswered queue: solved by OP (with help from Richjard)
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900