Alternative for Collider?

Hi. I want to make the player open a cabinet door when the trigger button is pressed while he overlaps the door. However, the code I’m using here only works if the very edges of both the player and the door touch each other. How can I make it work with the desired criteria? Thanks!

Code:

using UnityEngine;

public class CabinetDoor : MonoBehaviour
{
    // Start is called before the first frame update

    // Update is called once per frame
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (Input.GetKey(KeyCode.O))
        {
            if (collision.tag == "Player")
            {


                GetComponent<Animator>().SetTrigger("open");
            }
        }
    }
}

Really depends on your “criteria.”

If you just need to know the distance between two Vector3 points, use Vector3.Distance()

Why would it only work if only the edges touch? Box, Circle, Capsule and Polygon colliders are closed shape colliders so have a defined “inside” therefore any overlap on their interiors are detected. The only thing that isn’t are edges as produced by an EdgeCollider2D or CompositeCollider2D (in Outline mode). They don’t have an “inside” and only the edges are detected.

Before looking for another solution, maybe you should explain more about these colliders because something sounds wrong here.

Here’s probably the best way of explaining exactly what the problem is. I held “o” near the end. I also covered my character in a white rectangle for secrecy.

7891273--1004497--2022-02-12-10-01-29.gif

That really doesn’t explain anything unfortunately.

You’re explaining what you think are the problems, not what you’ve got set-up. We don’t know what colliders you’re using here, how they’re configured, how you’re moving the thing you’re moving etc.

All we can see is a script with an OnTriggerEnter2D which doesn’t really relate to anything.

On trigger enter is your problem. I have not used unity in years, I think there is something like on trigger stay or so. Or you use trigger enter / exit to determine if there is an overlap. There is also some api for collider overlap

Now I need to figure out how to disable the contents inside the cabinet until it opens

I stole the script off my refrigerator, the script that turns the light off when I shut the door.

You’re already tracking the state of the door, as my fridge was… just use that state use it to call .SetActive() on either a root GameObject of the cabinet’s contents, or else all of the content GameObjects individually, your choice.

https://docs.unity3d.com/ScriptReference/GameObject.SetActive.html

Ok, now I have a problem with trying to make the item obtainable ONLY when the cabinet door has been opened. However, I have tried numerous ways to try to make it work and had no success. I also had error messages saying “Object reference not set to an instance of an object”. This sounds like a complicated error to me. All I’m trying to do is tell the cabinet script to enable the collider component of the item whenever the cabinet is in the “open” state. How do get this to work??? What changes should I make to either code?

Item (HealthPickUp) code:

using UnityEngine;

public class HealthCollectable : MonoBehaviour
{
    [SerializeField] private float healthValue;
    public CabinetDoor cabinetDoor;
    private Collider2D collidertwodee;

    private void Start()
    {
        cabinetDoor = GetComponentInParent<CabinetDoor>();
        collidertwodee = GetComponent<Collider2D>();
    }

    // Start is called before the first frame update
    private void Update()
    {
        if (cabinetDoor.isItOpen == false)
        {
            collidertwodee.enabled = false;
        } else
        {
            collidertwodee.enabled = true;
        }
    }
    void OnTriggerEnter2D(Collider2D collision)
    {
            if (collision.tag == "Player")
            {

                collision.GetComponent<Health>().AddHealth(healthValue);
                gameObject.SetActive(false);

            }
        }
    }

Cabinet Code:

using UnityEngine;

public class CabinetDoor : MonoBehaviour
{
    public bool isItOpen;

    // Start is called before the first frame update
    private void Start()
    {
        isItOpen = false;
    }
    // Update is called once per frame
    private void OnTriggerStay2D(Collider2D collision)
    {
        if (Input.GetKey(KeyCode.O) && collision.tag == "Player")
        {
           
            GetComponent<Animator>().SetTrigger("open");
            isItOpen = true;
        }
           
    }
}

Oh no, simplest error there is. There’s even a pinned forum post it’s that common!

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Steps to success:

  • Identify what is null
  • Identify why it is null
  • Fix that

That does not help at all. It basically just tells me to “figure it out” like I’m some genius. The truth is, I HAVE tried figuring it out by trying out all sorts of methods just to get one object to “find” another and target its properties. But because of the blind nature of Unity, all of my vigorous attempts to get an object to find another result in failure and numerous error messages. I even set some crucial variables to public.

The OnTriggerStay function worked! Thanks!

I cannot even imagine what on earth you are talking about.

No, you just simply misunderstood the post above.

Somehow you have your mind wedged that this is a:

That’s incorrect. It’s an absolutely utterly trivial and common error.

It is literally equivalent to me pointing and saying “GET A COOKIE FROM THAT JAR!” and there is no jar.

That’s it. I’m not exaggerating.

Step 1- Identify the error.

When you read a line like this one:

MyScript2.Start () (at Assets/MyScript2.cs:11)```

You can instantly parse it and know the following about the nullref:

- it is in the file ```MyScript2.cs```
- it is on line 11
- it is within the function ```MyScript2.Start()```

If you don't pick up that skill pronto-quick you are seriously going to have a rough time programming.

Now you can move onto Step 2 and Step 3!

I actually got it now! I can’t believe how blind I was to the “public” slot in the inspector!