I have some 2D objects in same transform position. How can I find out how many objects are in one same position and which objects are they?
EDIT:
Here is my code:
I want save all object on transform.position - new Vector3(speed, 0, 0)
GameObject go = GetObjectAt(transform.position - new Vector3(speed, 0, 0));
public GameObject GetObjectAt(Vector3 position)
{
string pos = position.x + "_" + position.y + "_";
if (obstacleDictionary.ContainsKey(pos + "BigRed"))
{
return obstacleDictionary[pos + "BigRed"];
}
else if (obstacleDictionary.ContainsKey(pos + "SmallRed"))
{
return obstacleDictionary[pos + "SmallRed"];
}
else return null;
}
I think you can use List<SomeModel>
for keep object and postion.
Add your object to list when the object created
then find same postion object used by LINQ
CodeSample
Public class ObjectListener
{
public string ObjectName {get; set;}
public Vector2 ObjectVector {get; set;}
}
public class CreateObject : MonoBehaviour
{
List<ObjectListener> _gameObjectListener = new List<ObjectListener>();
void Update()
{
private GameObject _gameObject ; // you must create _gameObject
// and set tranform.Postion maybe you
// can use a method returned gameObject
Instantiate(_gameObject, _gameObject.transform.position, Quaternion.identity);
_gameObjectListener.Add(new ObjectListener
{
ObjectName = _gameObject.Name,
ObjectVector = _gameObject.transform.postion
});
}
}
I'm not sure that's the right way, but you can keep all created object name and postion in list object. So you can find how many object are in same object or not