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

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.

The issue was deeper than it appeared. My team created the object that is the Collider in this case, and they did some bad design. The player (Collider) had a nested sub-player with the typical third person scripts attached to him because the player ‘becomes’ that model at a point in the game. So the collision was happening twice - once for the thing it should, and once for the thing it shouldn’t collide with.

Bad design. Keep it out of your projects!