you might find working with “bounds” and “overlapbox/sphere” is a cleaner approach. Checking the “position” of the transforms doesn’t take into account the size of the things being compared.
What you have provided does not help. It does not convey how you want these prefabs to spawn. I’m assuming in a grid, just not fully filled, but what you wrote does not really seem to do that. It would be far easier to run through all the positions of the grid, and then choose to spawn an object or not, and then what to spawn.
//run through every z position
for(int z = 0; z < gridRows; z++){
//run through all x positions at the current z position
for(int x = 0; x < gridColumns; x++){
//choose whether to spawn an object
if(Random.range(0f, 1f) > spawnChance){
//choose object to spawn at current position
Instantiate (objectList(Random.Range(0, objectList.Lenght - 1)), new Vector3(x, 0, z), Quaternion.identity);
}
}
}
Should have been clearer sorry, what i’m going for is there’s a set size map (hence the min Z,Max Z ect…) and it will instantiate random prefabs in this area, if two overlap it will find a new position for the prefab
I’ve tested using bounds seperately, it works if one object goes into another but not vice versa, it just checks the object next on the list, in my case [0] checks for [1] but [1] doesn’t check for [0]
the code is here
using UnityEngine;
using System.Collections;
public class CheckBounds : MonoBehaviour {
// Use this for initialization
public Bounds[] bounds;
public GameObject[] objs;
void Start ()
{
bounds = new Bounds[objs.Length];
for (int i = 0; i < bounds.Length; i++)
{
bounds[i] = objs[i].GetComponent<BoxCollider>().bounds;
}
}
// Update is called once per frame
void Update ()
{
for (int i = 0; i < objs.Length - 1; i++)
{
if (objs[i].GetComponent<BoxCollider>().bounds.Intersects(bounds[i + 1]))
{
Debug.Log("pls");
}
}
}
}
Is there a way i can somehow check if it intersects with any object in the array, apart from itself?
If you are insistent on this method, that’s because you need to have a target to check against everything else. Without a target the script isn’t really doing anything useful.
I was thinking more “use bounds to work out an appropriate “size” box, overlapbox test that box which will be a test against all colliders in the scene, if nothing comes back all good, if something comes back, there’s something there”
EDIT: I don’t want objects to delete themselves, just searching for the same type of concept
Can anyone link me possible methods? I keep running into the same issue, even with adding a target. It doesn’t check the collision on instantiated prefabs…