Open door with a key - 2D

Hey guys,

I have a script called CoinPickupNew where it is set that a char picked up a key:
gotkey = true;

I have created a trigger empty game object that while the char has a key and when passed through a door will open:
using UnityEngine;
using System.Collections;

public class OpenDoor : MonoBehaviour {
	public GameObject door;

	void OnTriggerEnter2D(Collider2D col)
	{
		if (GetComponent<CoinPickupNew>().gotkey == true)
		{
			door.SetActive(false);
		}
	}
}

I drag the door sprite to the Door slot but still receive a Object reference (col) not set to an instance of an object error, while passing through trigger object.

if (col.GetComponent().gotkey == true)

You are trying to access the component CoinPickupNew on the player, but the player doesn’t have that component. This if block shouldn’t execute, nor should the else block. If you are actually getting “Registered” from

Debug.Log("Registered");

I’m really confused… you aren’t printing that same debug string somewhere else? :stuck_out_tongue:

In general, if you are trying to access a component on an object that doesn’t have that component, you’re going to get an error.