I create an object and wanna check collision this new object with others. cause i don’t want to place trees too close to each other. So there is a code from one script.
private IEnumerator GenerateTrees()
{
//yield return new WaitForSecondsRealtime(2f);
for (int i = 0; i < amountTrees; i++)
{
var border = sizeX - delta;
var pos = new Vector3(Random.Range(-border, border), 0, Random.Range(-border, border));
var tree = GenerateObject(treesPrefabs);
SetRandomPosition(tree, border);
yield return new WaitForEndOfFrame();
//yield return new WaitForSecondsRealtime(2f);
while (tree.GetComponent<CollisionDetector>().hasCollision)
{
print("while");
SetRandomPosition(tree, border);
yield return new WaitForEndOfFrame();
//yield return new WaitForSecondsRealtime(2f);
}
}
}
And there is from other which is attached for every prefab.
public class CollisionDetector : MonoBehaviour
{
public bool hasCollision;
private void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.layer == 6)
{
hasCollision = true;
}
}
private void OnTriggerExit(Collider collision)
{
if (collision.gameObject.layer == 6)
{
hasCollision = false;
}
}
}
It seems that code in the second can’t update in time. That’s why the first script doesn’t generate new position for tree. But when coroutine is done, i can see in inspector that hasCollision is true. Maybe there is a way to wait for end of the seconds cript update?