Search Unity

How to Use Triggers to Move Game Objects?

Discussion in 'Physics' started by FlurpityFlurp, Dec 12, 2018.

  1. FlurpityFlurp

    FlurpityFlurp

    Joined:
    Dec 8, 2018
    Posts:
    3
    Hi,

    Complete beginner here; just learning this stuff.

    I am trying to create a trigger that will affect another game object.

    Say I have cube A and cube B.

    When the player touches cube A, I'd like it to do some action to cube B (maybe move or destroy it).

    Every resource I've seen on triggers and collisions seems to only discuss 2 objects in direct contact w/each other.

    How can I set it up so that direct contact between the player and cube A has an effect on a remote cube B?

    If you can point me in the general direction that would be great.

    Thanks!!
     
  2. JuozasK

    JuozasK

    Unity Technologies

    Joined:
    Mar 23, 2017
    Posts:
    84
    As a simplest solution you could have a reference to cube B in your cube A script

    Then, when executing Cube A OnTriggerEnter function for when an object that is the Player enters it, you can say "Do x to Cube B"

    Roughly something like:
    class CubeA {
    [SerializeField]
    GameObject CubeB

    void OnTriggerEnter(collider col)
    {
    if (col.gameobject.tag == "player")
    Destroy(CubeB);
    }
    }

    And as for CubeB you'd pull the reference from the hierarchy into the script so you don't get a NullReferenceException
     
  3. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Code tags Mister Unity :D
    Ha Ha!
     
    xVergilx and JuozasK like this.
  4. FlurpityFlurp

    FlurpityFlurp

    Joined:
    Dec 8, 2018
    Posts:
    3
    Great, thanks very much for the pointer!