Collision2d error message saying "type: "
I have been trying to learn collisions for 2d but for some reason it is spitting out this message when I try to run.
Script error: OnCollisionEnter
This message parameter has to be of type:
The message will be ignored.
public static TreeCol Instance;
void awake(){
TreeCol.Instance = this;
}
void onCollisionEnter2D (Collision2D col){
if(col.transform.name == "Tree"){
Debug.Log("Hit tree");
//if(Input.GetKeyDown(KeyCode.W))
//Destroy(col.gameObject);
}
}
I have this attached to the player object and have a 2d boxcollider attached to the "tree" with a trigger, I commented out the destroy object while I tried to get unity to stop ignoring the code.
Answer by Wrymnn · Feb 08, 2016 at 09:01 AM
Hello,
the players collider should not be marked as Is Trigger when using OnCollisionEnter2D(), the Tree can or doesn`t have to be as trigger. I belive the function should be with Capital C as well, as: OnCollisionEnter2D.
Is the "Hit Tree" message printed into Console?
Is the name of object tree really Tree? You can also you tags, since when you create new objects, they always have postfix (Clone), so your If statement:
if(col.transform.name == "Tree")
would no longer work.
are both objects using 2D collider? (2D Box Collider for example)
does at least one of those objects have Rigidbody2D component attached?
The player is not marked as a trigger, only the tree is. Adding capitals does nothing unfortunately. The message is not printed in the console when colliding with the tree. The object is named Tree and is not a clone. Both the player and tree have collider, the player has a ridgedbody2d attached.
I switched it from looking for a transform to looking at the tag and it still gives the error message.
$$anonymous$$aybe this will help figure out the problem? https://youtu.be/jGNVpxrQIHQ
Fuh, haven`t seen anything wrong with what you have just showed, this is kinda strange.
Does player also has BoxCollider2D attached?
Don`t you have some class in your project named Collision2D? Event from plugin or 3-rd party code, this may be very likely cause
Try to write the function parameter as:
void OnCollisionEnter2D (UnityEngine.Collision2D col)
Sorry, other than that, I am out of ideas what may be wrong, if above doesn`t help, I may suggest you reporting the bug and attaching the project with this error to it. You can do it From Unity Editor upper tabs: Help -> Report a Bug...
Some how the error went away after a restart but the collision trigger still is not displaying "Hit Tree" when passing by.
I switched the circlecollider2d for a box collider2d on the player with no changes. There are on 3rd party scripts with collision in them.
I really appreciate you trying to help, it is super weird that this is happening.
how are you moving the player? with velocity, forces, or translate? the latter would be wrong
Your answer
![](https://koobas.hobune.stream/wayback/20220612063408im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Script not working on Colliders 0 Answers
How to check if any/multiple 2D sprites neighbour/collide with eachother 0 Answers
Is there a way to prevent my character colliding with background? 0 Answers
How to create 2D terrain procedurally with 2D physics colliders ? 0 Answers
With two box colliders on one object, how can I tell which one was hit? 1 Answer