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)
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);
}
}
}
Answer by IgorAherne · Nov 21, 2016 at 06:44 PM
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.
Your answer
Follow this Question
Related Questions
Create an array of scripts that don't affect each other? 1 Answer
Can I not Use Void Start this way with 2 animation references? 1 Answer
Issues trying to activate an image through OnTriggerEnter/Exit/Stay(Flickering) 1 Answer
How to call an action from a script that is on the anonymous object you collide with? 1 Answer
c# Object reference not set to an instance of an object 1 Answer