- Home /
How to make sure two Tiles not overlap each other? (Unity3d C#)
I'm trying to make a Grid who will be easily convertible between 2D and 3D and i'm having a problem with overlapping prefabs in the case of the 3D generation.
The thing is, the DataObjects (Tiles) actualy have inside them the prefab of the thing who must be in that tile, but the prefab is bigger than the Tile.
As you can see in the image, i have 3 Tiles selected, Each Tile have a prefab and all of them have the same kind of prefab, they are in the scene highlighted by the orange borders right now in the image.
Then you could imagine than, if the prefabs are equal between the 3 of them they should all have th same size, and that's exactly the case (i.e. in fact they have the same size) but the thing is they are overlaping each other, have that in consideration when looking at the image.
So the question will be...
How to make sure two Tiles not overlap each other?
based on a suggestion on working not with scale but pixels with units, i don't know exactly how to do that, but searching on google make me try this, which actualy doesn't work:
tile.tile_GO.GetComponent<Renderer>().bounds.size = thisPrefab.GetComponent<Renderer>().bounds.size;
It doesn't work because "bounds" is read only.
Do you know a work around to solve this?, or is another way to achieve what i'm looking for?
Feel free to ask, comment, request for clarification or even correct my grammar, all would be welcome if it is respectfull, specialy if it helps to find and answer.
The idea is stop the overlapping, if you know a way to make one next to the other one, or even leave a gap between them in runtime, it will be also consider a valid answer.
Thanks in advance.
PS: i'm using unity 2017.1.0f3, scripting in C#.
The idea is stop the overlapping, if you know a way to make one next to the other one, or even leave a gap between them in runtime, it will be also consider a valid answer
Answer by bgprocks · Jul 31, 2017 at 08:10 PM
temporarily unparent them, then resize the to match each other, then reparent them.
Doesn't work, i already write in the question than the problem is because bouds is READ ONLY, hence, i can't modify it, it doesn't matter if it is parented or not, thanks for the try anyway.
Answer by x4637x · Aug 01, 2017 at 02:59 AM
Have you try something like this ?
float x = tile.tile_GO.GetComponent<Renderer>().bounds.size/2;
thisPrefab.transform.position = thisPrefab.transform.position + new Vector3(x, 0, 0);
Or do you want them to keep current position?
Your answer
Follow this Question
Related Questions
How can I determine an object of prefab's size (for positioning when instantiating them) 2 Answers
Not a question - just some performance metrics for rendering from Unity scripts 0 Answers
Spawn Prefabs 1 Answer
Creating an array of prefabs? 4 Answers
Replacing empty gameobjects with prefabs 3 Answers