Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    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. }