Compare distance with prefab instantiated with other objects to change its position

Hello, I’m new to Unity and C #, I apologize in advance for English, I write with Google Translator …

I’m developing a game, based on the model you have on the Unity website, and an online course I’m doing, I’m having a detail to adjust, I’ll try to explain …

When I instantiate some prefabs dynamically (cubes), as the positions are random, sometimes it gets overloaded with other objects (walls), in case I need to check if it is superimposed with another object and change its position.

Can anyone give any tips on how I can do this? That is, check the distance between the new instabed instab, with another object that is already part of the scene?

This is code that instantiates objects:

void CriaObjetos(int item){
	//Camera c = Camera.main;

	for (int i = 0; i <= qtdeObjetos; i++){
		GameObject letraAleatoria  = letras[item];   
		Instantiate (letraAleatoria, new Vector3(Random.Range(-9,9), 0.4f, Random.Range(-9,9)), Quaternion.identity);

It has an image trying to get a better idea of how it gets installed.

Thanks for listening.102375-imgjogo02.png

If I understand correctly, your issue is that objects are being instantiated within other objects? In this case I believe the bounds of a collider or mesh is going to be your best bet. I would take your static objects, the ones that look like wood blocks in the image, and add them to an array or collection. Then on instantiation, loop through that collection and check if the newly instantiated object is within any of the colliders using Bounds.Contains. If that check returns true, simply move the object and check again until it is no longer true.