NullReferenceException: Object reference not set to an instance of an object

Hello,
I have a problem which I can no solve. I get this error: NullReferenceException: Object reference not set to an instance of an object

when I select something on my game the code is here.

using UnityEngine;
using System.Collections;

public class Buy : MonoBehaviour {

	public Coins coins;

	// Use this for initialization
	void Start () {
		coins = GetComponent<Coins> ();
	}
	
	// Update is called once per frame
	void Update () {
	

	}

	void OnMouseEnter()
	{
		renderer.material.color = Color.red;
	}

	void OnMouseExit()
	{
		renderer.material.color = Color.white;
	}

	void OnMouseUp()
	{
		if (coins.amountOfCoins == 10) 
		{
			GameObject.Find("Cost_2").renderer.enabled = false;
		}
	}

}

The error is at the if statement.

Any help would be appreciated.

The coins object is null at the IF statement, suggesting that the component lookup did not find a component of type Coins. In order for this to work with your current code, your Coins script (I assume it’s another script?) must be added to the same game object that the Buy script is on. If Coins is attached to a different game object, you’ll first need a reference to that object.

GameObject coinsParentObject = GameObject.Find("TheObjectName");
coins = coinsParentObject.GetComponent<Coins>();