Sorry, I realize this is a semi common question and I’ve looked at the others but can’t figure out. From what I understand, it’s telling me the script named Collectables doesn’t exist, or isn’t attached to that gameObject, but it definitely is.
I have the following script that is erroring on line 16,
collision.gameObject.GetComponent().addMint();
with the following error:
NullReferenceException: Object reference not set to an instance of an object
Collect.OnTriggerEnter (UnityEngine.Collider collision) (at Assets/Scripts/Collect.cs:16)
Here is my script:
using UnityEngine;
using System.Collections;
public class Collect : MonoBehaviour
{
public int type;
// 0-Mint
// 1-Cucumber
// 2-Salt
// 3-Yogurt
void OnTriggerEnter(Collider collision)
{
print("type = " + type);
if(collision.tag == "Player" || collision.tag == "Donkey")
{
collision.gameObject.GetComponent<Collectables>().addMint();
Nasreddin temp = collision.gameObject.GetComponent<Nasreddin>();
temp.collect(type);
Destroy (gameObject);
}
}
}
I’m printing type so I know it’s being initialized, so that’s definitely happening. Also, addMint() is being called correctly, I just want to get rid of this error.