IF statements not working correctly

I need help with if statements. When same 3 objects are positioned adjecent to each other they should be destroyed but for some reason the code only works when objects are possitioned on top of each other, if they are positioned horizontaly next to each other the code doesn’t work. Works the same with IFs or ELSE IFs. Does anybody have any ideas for solution?

The code:

if(gameObject.tag == topHit.transform.tag && gameObject.tag == bottomHit.transform.tag){
					Destroy(topHit.transform.gameObject);
					Destroy(bottomHit.transform.gameObject);
					Destroy(gameObject);
				}
else if(gameObject.tag == topHit.transform.tag && gameObject.tag == rightHit.transform.tag){
					Destroy(topHit.transform.gameObject);
					Destroy(rightHit.transform.gameObject);
					Destroy(gameObject);
				}
else if(gameObject.tag == topHit.transform.tag && gameObject.tag == leftHit.transform.tag){
					Destroy(topHit.transform.gameObject);
					Destroy(leftHit.transform.gameObject);
					Destroy(gameObject);
				}
else if(gameObject.tag == bottomHit.transform.tag && gameObject.tag == leftHit.transform.tag){
					Destroy(bottomHit.transform.gameObject);
					Destroy(leftHit.transform.gameObject);
					Destroy(gameObject);
				}
else if(gameObject.tag == bottomHit.transform.tag && gameObject.tag == rightHit.transform.tag){
					Destroy(bottomHit.transform.gameObject);
					Destroy(rightHit.transform.gameObject);
					Destroy(gameObject);
				}
else if(gameObject.tag == leftHit.transform.tag && gameObject.tag == rightHit.transform.tag){
					Destroy(leftHit.transform.gameObject);
					Destroy(rightHit.transform.gameObject);
					Destroy(gameObject);
				}
else {
//Do nothing
				}

Good day.

You need to debug the code, line by line, while running, to see what string tags is comparing at the “if” sentences, see if the code enters inside the “if”, etc…

DEtect what step is not doing what you expect, and I’m sure you will see the solution.

Left and right hit are right at the bottom of the priorities which could explain your troubles. Remember that if your code finds any of the above before it reaches the bottom, then that’s the code it will execute, skipping over the last ones. If you are simply checking for objects in a line, there may be easier solutions. Off the top, Physics2D.Linecast could be used or Physics2D.OverlapBox (Both have 3D variants if your game works in all three dimensions). Either way, it might help to provide more information on what you’re trying to do so people can give a more informed answer.