I have a “flora” script attached to the trees and i got a solution working that checks its
OnCollisionStay ```private void OnCollisionStay2D(Collision2D collision)
{
if(collision.gameObject.tag == "collisionMap")
{
Destroy(gameObject);
}
}```
but I want it to work with an extra function that gets called on the three object after I instantiate it in my Populate world script here
```public void spawnTrees()
{
for(int i = 0; i <= amountTrees; i++ )
{
Vector3 pos = center + new Vector3(Random.Range(-size.x / 2, size.x / 2), Random.Range(-size.x / 2, size.x / 2), 0);
GameObject tree = Instantiate(tree1, pos, Quaternion.identity);
tree.GetComponent<Flora>().checkCollision();
}
}
so it can eventually give feedback if the tree spawned properly and this attempt:
public void checkCollision()
{
Vector2 origin = new Vector2(myCollider.transform.position.x, myCollider.transform.position.y);
Vector2 size = new Vector2(myCollider.bounds.size.x, myCollider.bounds.size.y);
RaycastHit2D raycast = Physics2D.BoxCast(origin, size, 0f, Vector2.right, 1f);
if(raycast.collider != null) Debug.Log(raycast.collider.tag);
}
just did not register any collision and I dont know why.