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

Collder2D with 2 parametars?

Discussion in 'Scripting' started by Chunrand, Apr 5, 2015.

  1. Chunrand

    Chunrand

    Joined:
    Feb 21, 2015
    Posts:
    27
    Okay so I get this in the log:
    Script error: OnCollisionEnter2D
    The message must have 0 or 1 parameters.
    The message will be ignored.

    Can I make the OnCollisionEnter2D with 2 parametars?

    Code (CSharp):
    1.  
    2. private bool playerSwitchedSide;
    3.  
    4.     void OnCollisionEnter2D (Collider2D rightWall, Collider2D leftWall)
    5.     {
    6.         if (rightWall.gameObject.tag == "Right wall")
    7.         {
    8.             playerSwitchedSide = true;
    9.         }
    10.         else if (leftWall.gameObject.tag == "Left wall")
    11.         {
    12.             playerSwitchedSide = false;
    13.         }
    14.  
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    No, OnCollisionEnter2D can only take 0 or 1 parameters and it must be a Collision2D, not a Collider2D. If the object collides with multiple objects, Unity will call upon OnCollisionEnter2D several times. Each time there will be information about the "current" collision found in the Collision2D parameter.
     
  3. zoran404

    zoran404

    Joined:
    Jan 11, 2015
    Posts:
    520
    The parameter [Collider2D] is the collider component of the gameObject you hit.
    You should check if that single parameter is right or left wall:

    Code (csharp):
    1. private bool playerSwitchedSide;
    2.  
    3.     void OnCollisionEnter2D (Collision2D coll)
    4.     {
    5.         if (coll.gameObject.tag == "Right wall" ||  coll.gameObject.tag == "Left wall")
    6.         {
    7.             playerSwitchedSide = true;
    8.         }
     
  4. psyydack

    psyydack

    Joined:
    Aug 30, 2013
    Posts:
    93
    I think this is a bit off about your question, but if I understand you want to check what side you are colliding. You can use this :

    Code (CSharp):
    1.  void OnCollisionEnter2D(Collision2D  collision)
    2.      {
    3.          Collider2D collider = collision.collider;
    4.  
    5.          if(collider.name == "Wall")
    6.          {
    7.              Vector3 contactPoint = collision.contacts[0].point;
    8.              Vector3 center = collider.bounds.center;
    9.              bool right = contactPoint.x > center.x;
    10.              bool top = contactPoint.y > center.y;
    11.          }
    12.      }
     
  5. Chunrand

    Chunrand

    Joined:
    Feb 21, 2015
    Posts:
    27
    Thank you to all of you who replied.. I found other way.. I attached two different scripts to the both walls and called the functions from other script.. Thank you again..