- Home /
Check For Collision with anything in If-else
I haven't been able to find an answer for this as all google results are about OnCollisionEnter() and the like. I do not want it to check every update. I'm doing simple object placement based on the mouse position. When I click to place the object I want to run a check on some of its colliders to see if they are touching anything immediately after instantiation. In pseudo-code I want to do this
If(collider is touching anything){
Destroy(collider.gameObject);
}
Answer by getyour411 · Jan 25, 2014 at 06:47 AM
http://answers.unity3d.com/questions/41599/how-do-i-check-if-an-instantiated-object-collides.html
or Google: unity3d object collider instantiate for other examples
As I said in my question I would like to avoid using OnCollisionEnter as this would require me to make a separate script for the object with the collider. When the object gets instantiated I want to see if any of its children's colliders are colliding and if so destroy the child. It seems clumsy to use OnCollisionEnter() because I don't want it to check for collisions afterwards(it's just gonna be hit by a raycast). Thanks for your response
You can add the component via script, then remove it when you no longer need it. Are these children colliders actual Collisions, or triggers?
Sorry I haven't responded for some time. I wasn't able to work on my project for a week and almost forgot I posted this! I ended up solving my issue simply by using Bounds.Intersects() and a for() loop to iterate through all the objects in the level. I haven't done hardly any optimizations for it yet but this is the technique I was looking for. I was able to do this in a script on the parent object this way which is nice. It also only checks for collisions when it absolutely needs to. I knew I could do it all this way as I've done this in XNA before. I just wasn't sure if Unity had a different and/or better way to do it. Again, thanks for you help.
Bounds.Intersects() is a good solution, I keep forgetting about it as I haven't used it personally, but I see others recommend & reference it often as a good tool.
That answer is not a test as the OP ask, it's an event.
Answer by BrandonHi · Aug 10, 2016 at 03:09 PM
Perhaps you could use OnCollisionEnter if you use time.deltatime and only execute OnCollisionEnter like one second after the instantiation. Sorry I was too busy to test it myself but it might work. I'm new too
That answer is not a test as the OP ask, it's an event.
Your answer
![](https://koobas.hobune.stream/wayback/20220613131356im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Check Collision In Editor Mode 2 Answers
Endless Runner Ground Issue 0 Answers
Linecast ignoring mesh collider 1 Answer
Collsion without ririgibody 1 Answer