Search Unity

Question Get values from another script during runtime from update()

Discussion in 'Scripting' started by S4MA3L, Jul 3, 2020.

  1. S4MA3L

    S4MA3L

    Joined:
    Apr 17, 2020
    Posts:
    38
    I am trying to get the properties of a GameObject which is stored inside a script within it, and I want to get these properties during runtime during OnTriggerEnter() is there any way I can get these properties from that GameObject with whom I am colliding with?

    Thanks in advance.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    OnTriggerEnter includes a Collider parameter which is the collider that entered the trigger collider. You can get the GameObject from that or any other component on the GameObject by using GetComponent.
     
  3. S4MA3L

    S4MA3L

    Joined:
    Apr 17, 2020
    Posts:
    38
    Thanks, but when my object hits a trigger box collider 2d, the col.gameobject is not the collider object but my rigidbody player
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    If the script with OnTriggerEnter is on the object your player is colliding with, then you're already running code in the script you care about...

    In that context
    this.gameObject
    is the GameObject you care about.

    In other words:
    this.GameObject
    gives you this side of the collision.
    col.GameObject
    gives you the other side.

    If this is still confusing maybe share the code you have so far.
     
  5. S4MA3L

    S4MA3L

    Joined:
    Apr 17, 2020
    Posts:
    38
    Thanks you so much it solved the problem I've been having.