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. Dismiss Notice

Script error: OnTriggerEnter2D This message parameter has to be of type: Collider2D? (Solved)

Discussion in 'Scripting' started by Deleted User, Jul 30, 2019.

  1. Deleted User

    Deleted User

    Guest

    So made a basic bit of code for collision detection, that uses 2d box collider set as a trigger. Here's the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class CoinCollect : MonoBehaviour
    7. {
    8.     public GameObject Score;
    9.     private void OnTriggerEnter2D(Collider other)
    10.     {
    11.         Debug.Log("Collider Entered.");
    12.         if (other.tag == "Player")
    13.         {
    14.             Score.GetComponent<Scoreboard>().Coin();
    15.             Destroy(this);
    16.         }
    17.     }
    18. }
    19.  
    Whenever I run it, I get the message:
    Script error: OnTriggerEnter2D
    This message parameter has to be of type: Collider2D
    The message will be ignored.

    Anyone have any idea why?
     
  2. Team2Studio

    Team2Studio

    Joined:
    Sep 23, 2015
    Posts:
    98
    on line 9 the "other" parameter should be of type Collider2D
     
    Chezaus, Juengx and (deleted member) like this.
  3. Deleted User

    Deleted User

    Guest

    Yeah, noticed it. Feel more than a tad stupid now.
     
    Chezaus and Tidcy like this.
  4. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    I'm getting the same error, except I have the "collision" parameter set to Collider2D
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Show your code.
     
  6. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    I was able to get it fixed right after posting that, thanks though
     
  7. t4nm4y

    t4nm4y

    Joined:
    Mar 26, 2022
    Posts:
    1
    how did u fix it, since I'm also getting the same error msg, and the collider is not working, here's my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class collector : MonoBehaviour
    6. {
    7.     private void OnTriggerEnter2D(Collider2D collision)
    8.     {
    9.         if(collision.CompareTag("enemy"))
    10.         {
    11.             Destroy(collision.gameObject);
    12.         }
    13.     }
    14. }
    15.  
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    I'd like to see this error you're saying is the same. I say this because it should be clear that the OP has used a 2D callback "OnTriggerEnter2D" which as the docs show, accepts a 2D collider "Collider2D" and not a 3D collider i.e. "Collider".

    That is stated above though already.

    Can you post your error and the script it says is causing this then? Really though, you should create your own thread with your details including the full error message which includes type, line, column etc.