OnTriggerEnter not firing for objects with the same tag,How to detect multiple instances of the same tag using OnCollisionEnter
Hi all. I'm working on a game where you push a bunch of spheres inside a hole. Each sphere is an instance of a prefab and they all have the same tag and the same script below.
I'm trying to use OnCollisionEnter to detect when each of the balls touch the hole and increment count, but I only get the event called once, twice maximum, even though I have dozens of balls colliding with the hole, and the count is not incremented as it should, hence never entering the if.
Is there a way make sure I capture each of the collisions from each of the balls?
private void OnTriggerEnter(Collider other)
{
Debug.Log("Ball hit: " + other.gameObject.tag);
if (other.gameObject.tag == "Hole")
{
count++;
Debug.Log("count: " + count);
if (count > 5)
{
DoSomethingHere();
}
}
}
Appreciate your help!
Answer by zereda-games · Feb 16, 2019 at 03:35 AM
see if this works?
[Header("OnTrigger Control:")]
[Space]
[Tooltip ("What is the Tag on the Objects we want to find in the scene?")]
[TextArea(1)]
[SerializeField]
private string Tag="Hole";
[Space]
[Tooltip ("If we are using as a trigger, is it a trigger enter or trigger exit?")]
public bool IsTriggerExit=false;
private void OnTriggerEnter(Collider other)
{
if(GameObject.FindObjectsWithTag(Tag).Contains(other.gameObject)){
count++;
Debug.Log("count: " + count);
if (count > 5)
{
if(Equals (IsTriggerExit,false)){
Debug.Log(string.Format("We made it to {0} with: ", count ) + other.gameObject.name)+"(Enter)");
}
DoSomethingHere();
}
}
}
private void OnTriggerExit(Collider other)
{
if(GameObject.FindObjectsWithTag(Tag).Contains(other.gameObject)){
Debug.Log("count: " + count);
if (count > 5)
{
if(Equals (IsTriggerExit,false)){
Debug.Log(string.Format("We made it to {0} with: ", count ) + other.gameObject.name+"(Exit)");
}
DoSomethingHere();
}
}
}
Looks interesting and I really appreciate your help. I'm not a very experienced developer though and will test it once I get home in a few hours.
Just to confirm, I have multiple balls, one single hole. This script is inside each of the balls, not the hole. $$anonymous$$y intention was to check if each of the balls has collided with the hole. Is that what is does?
Thanks again!
not quite, with this code, it should be on your hole detecting the balls on Collission aka when the ball is touching the collider.. i'm pretty sure i forgot to put a check in to make sure it downt just jump to 4 right away.... Ohh poop i did too, i have the OnTriggerEnter and OnTriggerExit Backwards.. move line 17 to line 32 to fix the problem. Each time the ball exits the collision aka gets destroyed for example then the point on the Hole adds 1 point otherwise as is, as long as the ball is touching the collider the points will add up to 4 and stop because we have a check to make sure the number doesn't exceed 4. i don;t think what you want is it to be on the ball myself but there are many ways to do things and everyone has their way of doing things.
doing my fix would work too, all you would need to add is when the collisions hits 4 = true then don't spawn any more balls. as the player has used them all up.
so in an Update you would need something like:
public Transform Parent;
public GameObject Ball;
protected int balls=4;
protected int playerScore;
private static count=0
protected const string Save$$anonymous$$ey="SaveData";
void OnEnable(){
balls=4;
playerScore=0;
}
void Update(){
if(Equals(balls,0)){
InitializeGameOver(playerScore);
return;
}
if(Equals(balls<=0)||Equals(ball>=4))
if(Imput.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space){
instantiate(BallPrefab,Parent.position,Parent.rotation, Parent);
balls--;
}
}
}
void InitializeGameOver(int score){
SaveScore(score);
OnDisable();
}
void SaveScore(int score){
count++;
PlayerPrefs.SetInt(Save$$anonymous$$ey+count, score)
}
void OnDisable(){
//Do something
}
Answer by gabrielclapclap · Feb 16, 2019 at 06:00 PM
I got it!!! Such a beginner's mistake...
The count variable was being created inside each of the ball objects, hence it was never incremented as each object had it's own counter.
I created a counter inside a different class and started incrementing it from each of the balls.
BOOM!
Thank you!
ahahha That would do it LOL and upvotes are always appreciated ;)