Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Colliders on instantiated gameobjects

Discussion in '2D' started by unity_zHDYWyPXY60Zkg, Nov 1, 2018.

  1. unity_zHDYWyPXY60Zkg

    unity_zHDYWyPXY60Zkg

    Joined:
    Nov 1, 2018
    Posts:
    1
    Hi guys,

    I'm spawning a couple trees ramdomly on a map but I would like delete them in case they spawned on top of each other but my colliders don't seem to be working right. What's the right way of checking if a tree is colliding with another one when spawning it?

    This is my spawner code:
    upload_2018-11-1_11-45-8.png

    This is the script I tried to use in each tree to check for collision:
    upload_2018-11-1_11-45-50.png

    Thanks guys!
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    "other" is the the other object that has collided with this object. All you need is a way of identifying whether this "other" object is a tree GameObject.
    You can do this however way you wish, usually it's done by comparing the name or tag.

    For example:
    Code (CSharp):
    1. void OnTriggerEnter(Collider other) {
    2.    if(other.gameObject.CompareTag("tree")) {
    3.       //A collision with a tree object has been detected.
    4.    }
    5. }