BoxCollider2d.bounds.Intersects Not Working

Hi all, I am trying desperately to get BoxCollider2d.bounds.Intersects working and it is not happening at all. I am using this is a basic example: Unity - Scripting API: Bounds.Intersects

I think the script linked is outdated but I changed the parts to GetComponent and it still doesn’t work, intersection isn’t detected. I should also note that I have tried attaching 2d rigid body to my main character, marked as trigger, unmarked as trigger and it makes zero difference. Please help me. Thank you for reading and for your time. The script is attached to an empty game object. I am also aware that GameObject.Find is bad, I won’t do that a lot. :slight_smile:

My code is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class checking_bounds : MonoBehaviour {
    
    public Collider2D object_that_can_be_entered_collider;
    public Collider2D maincharactercollider;
    public GameObject maincharacter;
    public GameObject object_that_can_be_entered;

    // Use this for initialization
	void Start () {
        maincharacter = GameObject.Find("MainCharacter");

        object_that_can_be_entered = GameObject.FindGameObjectWithTag("CanEnterObject");
        if (maincharacter != null)
            maincharactercollider = maincharacter.GetComponent<BoxCollider2D>();
        Debug.Log(maincharacter.GetComponent<BoxCollider2D>().bounds); // this works 

        if (object_that_can_be_entered != null)
            object_that_can_be_entered_collider = object_that_can_be_entered.GetComponent<BoxCollider2D>();
        Debug.Log(object_that_can_be_entered_collider.GetComponent<BoxCollider2D>().bounds); // this works 
            
	}
	
	// Update is called once per frame
	void Update () {
        
        //Debug.Log(hero.GetComponent<BoxCollider2D>().bounds);

        if (maincharactercollider.GetComponent<BoxCollider2D>().bounds.Intersects(object_that_can_be_entered_collider.GetComponent<BoxCollider2D>().bounds))
        {
            Debug.Log("Bounds intersecting"); // this doesn't work
        }

	}
}

Keep in mind that the Bounds struct is a pure 3d construct. Also keep in mind that it does not represent the actual collider but it’s AABB (axis aligned bounding box). Your two bounds might not be at the same z distance or might have a size of 0 along z.

void Update()
{
    Bounds charBounds = maincharactercollider.bounds;
    charBounds.extents += Vector3.forward * Mathf.Infinity; // scale the bounds to infinity on z

    Bounds objBounds = object_that_can_be_entered_collider.bounds;
    objBounds .extents += Vector3.forward * Mathf.Infinity;

    if(charBound.Intersects(objBounds))
    {
        // [ ... ]

Thank you so much for replying!! This gets me closer. If I change both game objects to have the same z distance with my original code it works. If I use your code and I put my one game object back to z = 1 (instead of 0, same as other game object) it does not work. I need the game objects to be at different z depths because I’m using OnMouseDown. Is there anyway to work around this? Thank you again so much I appreciate it, a lot!! At the end of the day I need to be able to figure out if two game objects overlap.

@Bunny83 - Yep, orthographic looking straight on at the x-y plane. I’m prototyping a top down game. I tried your code again I am getting the same result as before. Thanks for sticking with me on this. I think I’ll be able to work around this by setting a boolean to fire off other code I need based on if an intersection as happening or not. It would have taken me a lot longer to figure out the z-axis on my own because I wrongly assumed a BoxCollider2D disregards the z-axis, and it looks like it doesn’t.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class checking_bounds : MonoBehaviour {
    
    public Collider2D object_that_can_be_entered_collider;
    public Collider2D maincharactercollider;
    public GameObject maincharacter;
    public GameObject object_that_can_be_entered;

    // Use this for initialization
	void Start () {
        maincharacter = GameObject.Find("MainCharacter");

        object_that_can_be_entered = GameObject.FindGameObjectWithTag("CanEnterObject");
        if (maincharacter != null)
            maincharactercollider = maincharacter.GetComponent<BoxCollider2D>();
        Debug.Log(maincharacter.GetComponent<BoxCollider2D>().bounds); // this works 

        if (object_that_can_be_entered != null)
            object_that_can_be_entered_collider = object_that_can_be_entered.GetComponent<BoxCollider2D>();
        Debug.Log(object_that_can_be_entered_collider.GetComponent<BoxCollider2D>().bounds); // this works 
            
	}
	
	// Update is called once per frame
	void Update () {
        
        Bounds charBounds = maincharactercollider.bounds;
        charBounds.extents += Vector3.forward * Mathf.Infinity; // scale the bounds to infinity on z

        Bounds objBounds = object_that_can_be_entered_collider.bounds;
        objBounds.extents += Vector3.forward * Mathf.Infinity;

        if (charBounds.Intersects(objBounds))
        {
            Debug.Log("Bounds intersecting"); // doesn't trigger
        }
         /*
        if (maincharactercollider.GetComponent<BoxCollider2D>().bounds.Intersects(object_that_can_be_entered_collider.GetComponent<BoxCollider2D>().bounds))
        {
            Debug.Log("Bounds intersecting"); // this now works if both z-axis is set to zero with my original code
        }
        */
	}
}