Hi, I’m working on a game similar to 3 stone match games. Mine is a little bit different. In this game, I have stones and birds. If a stone is colliding with 2 birds, those 3 objects gets destroyed and It’s not only just checking if it’s vertical or horizontal. It accepts both axis as long as a stone is colliding with 2 birds.
I have been working on this project for several weeks. I managed to overcome most of the issues I’ve faced (and managed to overcome some of them thanks to you guys.) Now there’s one more thing in my way and I can’t seem to find a way to deal with it.
When there are 2 stones that share the same birds like this :
Stones are brown, birds are blue
![Stones are brown, birds are blue][1]
4 objects gets deleted when only 3 of them are supposed to. I couldn’t find a single leak in my script and I have revised my script a thousand times. Here’s the script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Stone2Birds : MonoBehaviour {
bool access;
List<GameObject> allObjs = new List<GameObject>();
List<GameObject> allStones = new List<GameObject>();
List<GameObject> stoneBirds = new List<GameObject>();
void Permission(bool access)
{
this.access = access;
}
void Update()
{
if (access)
{
// ASSIGNS OBJECTS TO THEIR LISTS
foreach (Collider col in Physics.OverlapSphere(new Vector3(2, 5, 0), 6.50f))
{
if (col.gameObject.tag == "Stone")
{
allStones.Add(col.gameObject);
}
}
// CHECKS EACH STONE
for (int i = 0; i < allStones.Count; i++)
{
stoneBirds.Add(allStones*);*
for (int j = 0; j < allObjs.Count; j++)
{
if (allObjs[j].tag == “Bird”)
{
if (stoneBirds.Count < 3)
{
if (allObjs[j].transform.position.x - 1 == allStones_.transform.position.x && allObjs[j].transform.position.y == allStones*.transform.position.y)
{
stoneBirds.Add(allObjs[j]);
}*_
else if (allObjs[j].transform.position.x + 1 == allStones_.transform.position.x && allObjs[j].transform.position.y == allStones*.transform.position.y)
{
stoneBirds.Add(allObjs[j]);
}*_
else if (allObjs[j].transform.position.y - 1 == allStones_.transform.position.y && allObjs[j].transform.position.x == allStones*.transform.position.x)
{
stoneBirds.Add(allObjs[j]);
}*_
else if (allObjs[j].transform.position.y + 1 == allStones_.transform.position.y && allObjs[j].transform.position.x == allStones*.transform.position.x)
{
stoneBirds.Add(allObjs[j]);
}
}*_
* if (stoneBirds.Count == 3)*
* {*
* Destroy(stoneBirds[0]);*
* Destroy(stoneBirds[1]);*
* Destroy(stoneBirds[2]);*
* }*
}
}
stoneBirds.Clear();
}
allStones.Clear();
allObjs.Clear();
}
}
}
Thanks for reading.
[1]: http://i1063.photobucket.com/albums/t517/NikkiTheNikolai/image_zps5b6591c9.png?t=1391177608