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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Collider2D.IsTouching (Collider2D other) Error

Discussion in 'Scripting' started by Dev_23, Aug 8, 2015.

  1. Dev_23

    Dev_23

    Joined:
    Oct 4, 2014
    Posts:
    18
    Code (CSharp):
    1.         //Death
    2.         if (Collider2D.IsTouching(Collider2D other) {
    3.             if (other.gameObject.tag == "Death") {
    4.                 transform.position = new Vector2 (-6,0);
    5.                 LivesLeft --;
    6.             }
    7.         }
    This is not liking the "other" in both if statements (the first "other" is underlined in red, the second's text color is red). What am I doing wrong?
     
  2. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    This is not the same as function. In Function collider other, colliding objects send info about objects while in this if statment you just test variables. You could store thoese variables in colliding function
     
  3. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    You have two options:
    First:
    Code (CSharp):
    1. public Collider2D death; //assign GameObject with "Death" tag
    2.  
    3. ...
    4. if (Collider2D.IsTouching(death) {
    5.    //Do something
    6. }
    Second:
    Code (CSharp):
    1. void OnCollisionEnters2D (Collider2D other) {
    2.    if (other.gameObject.tag == "Death") {
    3.       //Do something
    4.    }
    5. }
     
    Dev_23 likes this.
  4. Dev_23

    Dev_23

    Joined:
    Oct 4, 2014
    Posts:
    18

    I used the first example and I am now getting an error.

    I have the reference as this:
    Code (CSharp):
    1. public Collider2D Death;
    2.  
    3.     void Start() {
    4.  
    5.         Death = GameObject.Find ("Death").GetComponent<Collider2D> ();
    6.     }
    Is my reference wrong?
     
  5. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    Well you refferencing is right, maybe it is wrong in part isTouching, or maybe at start of this object, object death is not predent, also i think it is faster and better to find with tag
     
  6. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    The problem is that you're trying to call the IsTouching function on the class and not on an object of that class. If a collision happens, you need to check if the colliding object is the "death" object, right? That means if the colliding object is called "other", then you need to do "other == Death" or "GetComponent<Collider2D>().IsTouching(Death)" or something.