- Home /
How to not instantiate an object if there is another object there?
Alrighty this might be a tough question, I searched around online but the only question on this that I found ended up not having a right answer, so here goes. I have a random generation thing set up where basically, when the scene starts, one room is randomly instantiated. Then that room creates hallways coming off of it, and those hallways create rooms on the end of them, and those rooms create more hallways, on and on until a certain limit is reached. The obvious problem that I have here is rooms being instantiated on top of other rooms, or hallways cutting through other hallways, etc. And obviously, this won't work well for a video game. So I can't post all 5 scripts I have that do this, but basically this is the line of code I am using to instantiate whatever object.
Instantiate(chosenDungeon,spawnPos, Quaternion.identity);
What I want is to check before doing this code whether or not there is already an object where it is going to instantiate, or whether or not it is going to overlap another object. I can't just check for an object at the spawnPos, because that won't stop a hallway from overlapping another hallway. I need it to check whether or not it will overlap another object before it instantiates. Is there any possible way to do this? Thanks in advance, I know this is probably a tough question.
Answer by KellyThomas · Jan 11, 2014 at 04:24 AM
`renderer.bounds` should do it:
With this axis aligned bounding you can perform an `Intersects()` check against each of your other dungeon segments.
Your answer
Follow this Question
Related Questions
Instantiated GameObject Not Rendering 1 Answer
How can I add the OnTriggerEnter function to all game objects that I instantiate? 1 Answer
C# instantiate into list 2 Answers
Distribute terrain in zones 3 Answers
Instantiate prefab at specific path 1 Answer