How many colliders i'm colliding with

Could someone give me an idea of how to check how many colliders with the specific tag my object is touching currently, without using CollsionEnter.
I tried making a value, and adding +1 each time CollisionEnter is called, and doing -1 when CollisionExit is called. And it works but when i do some other stuff, the value i made just stays example 4 eventhough it’s not touching any colliders currently. So is there another method ?

Unity collisions can sometimes be finicky and difficult to track with CollisionEnter/Exit messages.

One thing you can do is to track each object of interest to see whether they are involved in a CollisionEnter or CollisionStay event that frame. So if GameObject A has not entered the collider this frame, and is not staying within the collider this frame, therefore it must be not currently colliding. This way you don’t need to rely on CollisionExit event which may sometimes not fire.

Taking the event execution order into account:

//make a list to track collided objects
List<Collider> collidedObjects = new List<Collider>();

void FixedUpdate() {
     collidedObjects.Clear(); //clear the list of all tracked objects.
}


// if there is collision with an object in either Enter or Stay, add them to the list 
// (you can check if it already exists in the list to avoid double entries, 
// just in case, as well as the tag).
    void OnCollisionEnter(Collision col) 
   {
         if (!collidedObjects.Contains(col.collider) && col.collider.tag == desiredTag) 
        {
              collidedObjects.Add(col.collider); 
         }
    }

void OnCollisionStay(Collision col) {
     OnCollisionEnter(col); //same as enter
}


void Update() {
var numberOfColliders = collidedObjects.Count; // this should give you the number you need
collidedObjects.Clear(); // You can also clear the list here
}

If the above method does not work, you may also want to try the answer here.

public class crafting : MonoBehaviour
{
public float objects;

    void OnTriggerEnter(Collider collision){
      if(collision.gameObject.tag == "your tag"){
        iron = iron + 1;
      }
    }
    void OnTriggerExit(Collider collision){
      if(collision.gameObject.tag == "your tag"){
           iron = iron - 1;
      }
    }


    void Update()
    {
        Debug.Log(objects);
    }
}

This is WAY easier.

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

public class hover : MonoBehaviour {

	List <Collider> collidedObj = new List<Collider> ();
		
	/*In single cycle script execution it counts number of Object entered and exited that is stored in collider list.
	I made some hover spheres which collids with triggerd box collider
	this works but I have no idea about it's accuracy*/
	void OnTriggerEnter(Collider obj)
	{
		collidedObj.Add (obj.GetComponent<Collider>());
	}

		void OnTriggerExit(Collider obj)
	{
		collidedObj.Remove (obj.GetComponent<Collider>());
	}

	void Update()
	{
		int objCollidedCount = collidedObj.Count;
		print (objCollidedCount);
	}
}

//If anyone has better script please post here…

Count 3 different objects with tags. Have fun :smiley:

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

public class Counter : MonoBehaviour
{
    // Text buttons
    public Text CounterBlueText;
    public Text CounterRedText;
    public Text CounterYellowText;

    private int blueCount = 0;
    private int redCount = 0;
    private int yellowCount = 0;

    private int countRed;
    private int countBlue;
    private int countYellow;


    private void Start()
    {
        blueCount = 0;
        redCount = 0;
        yellowCount = 0;

    }
    void OnTriggerEnter(Collider collision)
    {
        if (collision.gameObject.tag == "Blue")
        {
            blueCount += 1;
            CounterBlueText.text = "Blue : " + blueCount;
        }

        if (collision.gameObject.tag == "Red")
        {
            redCount += 1;
            CounterRedText.text = "Red : " + redCount;
        }

        if (collision.gameObject.tag == "Yellow")
        {
            yellowCount += 1;
            CounterYellowText.text = "Yellow : " + yellowCount;
        }

    }

}

![190760-count.png|1859x1073](upload://5HBDriyOXIqVVyA55vlJiA3m4SU.png)