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

Reference collider from another script?

Discussion in 'Scripting' started by SuperCrow2, Jun 12, 2020.

  1. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    I need to reference a collider thats in another script in the one I am currently working on, doing this: this.GetComponent<Collider2D>() gets the collider for the script its attached to.

    How would I reference another collider in that script where the script is not attached to?
     
  2. dahiyabunty1

    dahiyabunty1

    Joined:
    Jan 22, 2020
    Posts:
    68
    findgameobjectwithtag("player").getcomponent<boxcollider2d>();

    change tag with your tag name of object
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You're mixing up your terminology here a bit.

    Your scripts are attached to GameObjects. You want to get a component that is attached to a different GameObject than the one your script is attached to. There are many ways to do this, but essentially what you need is a reference to that other GameObject, or directly to that Collider2D component.

    If both of these objects exist in the scene at edit time, you can simply create a public field:
    Code (CSharp):
    1. public Collider2D TheOtherCollider;
    Then you can drag and drop the object that contains the collider you care about from the scene into that field in the inspector.
     
    katfawaz64 likes this.
  4. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    I am trying to disable both teleporters after using one:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Teleporter1 : MonoBehaviour
    6.  
    7.     //left side teleporter
    8.     //teleports to right side
    9. {
    10.  
    11.  
    12. [SerializeField] Transform Teleport2; //player teleports
    13.  
    14.  
    15.  
    16.  
    17.     void OnTriggerEnter2D(Collider2D col)
    18.     {
    19.         col.transform.position = Teleport2.position; //teleports to right side
    20.         this.GetComponent<Collider2D>().enabled = false;  //disabled after its used
    21.         StartCoroutine("Enable"); //enabled again after 5 sec.
    22.  
    23.     }
    24. private IEnumerator Enable()  //this enables it after 5 seconds
    25.                               //to disable after an amount of time, change true to false
    26.     {
    27.         yield return new WaitForSeconds(5f);
    28.         this.GetComponent<Collider2D>().enabled = true;
    29.     }
    30. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Teleporter2 : MonoBehaviour
    6.  
    7. //right side teleporter
    8. //teleports to right side
    9. {
    10.  
    11.  
    12.  
    13.     [SerializeField] Transform Teleport1; //player teleports
    14.  
    15.  
    16.     private void Start()
    17.     {
    18.         this.GetComponent<Collider2D>().enabled = false;
    19.         StartCoroutine("Enable"); //enabled again after 5 sec.
    20.  
    21.     }
    22.  
    23.  
    24.  
    25.     void OnTriggerEnter2D(Collider2D col)
    26.     {
    27.         col.transform.position = Teleport1.position; //teleports to left side
    28.         this.GetComponent<Collider2D>().enabled = false;  //disabled after its used
    29.         StartCoroutine("Enable"); //enabled again after 5 sec.
    30.        
    31.     }
    32.  
    33.     private IEnumerator Enable()  //this enables it after 5 seconds
    34.                                   //to disable after an amount of time, change true to false
    35.     {
    36.         yield return new WaitForSeconds(0.5f);
    37.         this.GetComponent<Collider2D>().enabled = true;
    38.     }
    39. }
    40.  
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Ok it looks like you already have a reference to the other teleporter then.

    Just get the component on the other teleporter. For example:
    Code (CSharp):
    1. Teleport2.GetComponent<Collider2D>()
     
  6. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    it worked, thanks! I didn't realize how simple it was by just having to change "this" to the game object that I want haha.

    I also had had to do it with the teleporter 2 script but change "this" to "teleport1".
     
  7. bakedpizaa

    bakedpizaa

    Joined:
    Feb 27, 2022
    Posts:
    3