Error: Object reference not set to an instance of an object dailyReward.OnTriggerEnter (UnityEngine.Collider col)

Hello!

Im getting this error:

NullReferenceException: Object reference not set to an instance of an object
dailyReward.OnTriggerEnter (UnityEngine.Collider col) (at Assets/Scripts/dailyReward.cs:15)
82606-screen-shot-2016-11-21-at-75043-pm.png

Code A:

using UnityEngine;
using System.Collections;

public class playerMoney : MonoBehaviour {
	public int myMoney = 100;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

The error code:

using UnityEngine;
using System.Collections;

public class dailyReward : MonoBehaviour {
	private playerMoney money;

	// Use this for initialization
	void Awake () {
		money = GetComponent<playerMoney> ();
	}
	

	void OnTriggerEnter (Collider col){
		if (col.gameObject.tag == "car") {
			money.myMoney += 50;
			Debug.Log ("Daily Reward + 50€");
			Destroy (this.gameObject);
		}

	}

}

sais that the error is on “line 15”.

NullRefException is thrown because money is null, so null will not be able to provide you with “myMoney”, hence the error.

This means

void Awake () {
         money = GetComponent<playerMoney> ();
     }

didn’t succeed to get playerMoney component from the gameObject to which dailyReward script is attached. Make sure playerMoney is attached to the same gameObject.