- Home /
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 ?
Answer by taylank · Jan 10, 2016 at 06:17 PM
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.
I'm really sorry but i solved my problem in a diffrent way already, after i posted the question i just worked on it and figured out a solution. But this might be helpful if anyone else runs into the same thing. Thanks anyways !
Why use a list? Just use an int that you increment or decrement
Of course not, a list is necessary to check the collider presence, for those already there. Leaving it as an integer will increment the number every frame since it's called in collisionStay.
As for collisions, call FixedUpdate, not Update, physics is recalculated at FixedUpdate.
Answer by TheCookieMan_ · Aug 26, 2020 at 03:53 PM
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.
Answer by prakharexjailia · Mar 13, 2017 at 09:49 AM
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..
$$anonymous$$aybe check to see if the collided object already exsists on the list before adding again?
Answer by Razvan_Savin · Jan 04 at 05:34 PM
Count 3 different objects with tags. Have fun :D
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;
}
}
}
![alt text][1]
[1]: /storage/temp/190760-count.png
Your answer