- Home /
overlapping object doesn't disable
I want to have objects around another object. For example a cube with layer 9 and four cubes at each side except the top and bottom side, those cubes uses layer 10. If a cube with layer 10 is at the same position as another layer 9 cube or another layer 10 cube it should disable the meshrenderer. I know the logic, but I fail every time when I want to create it.
Scripts I used:
First script is used to run through all tiles and disable the meshrenderer, boxcollider and script to instantiate a tile (object).
public void RunThroughTiles() {
foreach (GameObject tile in tileObjects)
{
foreach (GameObject tile1 in tileObjects)
{
if (tile.transform.position == tile1.transform.position && tile1.layer == 10 && tile != tile1) {
tile1.GetComponent<MeshRenderer>().enabled = false;
tile1.GetComponent<BoxCollider>().enabled = false;
tile1.GetComponent<InstantiateTile>().enabled = false;
Debug.Log("Done");
}
if (tile.transform.position == tile1.transform.position && tile1.layer == 9 && tile != tile1) {
tile.GetComponent<MeshRenderer>().enabled = false;
tile.GetComponent<BoxCollider>().enabled = false;
tile.GetComponent<InstantiateTile>().enabled = false;
Debug.Log("Done1");
}
}
}
}
I have a list of objects named tileObjects every time a tile (object) got instantiated it adds it to that list.
I don't know what I do wrong.
Pictures:
The first picture is how it looks now:
The second picture is how it should look (I did this manually):
The transparent green objects (they uses layer 10) can be pressed to instantiate a land tile (land tiles uses layer 9).
I want to always have a circle of green transparent objects (the same as on the second picture) around a land tile, but not if the green transparent object would overlap something else. How?
You have to know: if a land tile is instantiated the green objects around it while be instantiated too.
Answer by kaosermail · Dec 12, 2018 at 03:29 PM
If you use the transform.position you are using the center of the object (and not the full object as you would like, if I haven't understood wrong). What you could do is use the box colliders to detect when an object is overlapping another object. Try using OnTriggerEnter() method.
I can't try that now, but it sounds obvious, thanks for your reply.
It kind of works, but when two objects with layer 9 hit each other they both got deleted any idea for that?
Check if the other.collider.layer is not equal to gameObject.layer before destroying.
When checking if the gameObject.layer is not equal to other.gameObject.layer before destroying it, they don't destroye each other anymore at the same time, but as you can see the objects with layer 10 don't get disabled when they are overlapping with objects with the same layer. ![alt text][1]
The lighter green means it's overlapping with other green transparent green objects. [1]: /storage/temp/129401-schermopname-21.png
Your answer
Follow this Question
Related Questions
overlapping object doesn't disable 0 Answers
Checking if object intersects? 1 Answer
Instantiate an object every x meters between two others. 1 Answer
Rotate the Object 90 Degrees 1 Answer
Distribute terrain in zones 3 Answers