- Home /
Script not recognizing collision
I'm a new unity dev who is currently watching Brackeys tutorial. After finishing episode 5, I decided to add the ability to jump, It didn't go as planned. (I'm still new to C#) I ended up just deleting the extra code. But after doing this my collision script stopped recognizing collision. My code is exactly the same as his, (Or at least I think it is), Any ideas as to why this is happening?
public class PlayerCollision : MonoBehaviour {
public PlayerMovement movement;
void OnCollisionEnter (Collision collisionInfo)
{
if (collisionInfo.collider.tag == "Obstacle")
{
movement.enabled = false;
Debug.Log("hit!"); // This does nothing
}
}
}
What stopped working? is your debug log message not appearing? Try putting another debug message right at the start of OnCollisionEnter before your if-statement to see if unity is registering the collision at all.
Answer by MangoliaStudios · Feb 18, 2021 at 10:39 PM
after the word monobehaviour press enter once, you got one of these ( { ) in the wrong place, syntax error
Is this what you meant? It didn't work
public class PlayerCollision : MonoBehaviour
{
public PlayerMovement movement;
void OnCollisionEnter (Collision collisionInfo)
{
if (collisionInfo.collider.tag == "Obstacle")
{
Debug.Log("hit!");
}
}
}
Hard to say without having the whole proj but it may be your if function havign that space breaking that line also yes to earlier comment. if this doesnt work ill have to take a look at the vid myself or yall can wait for a more seasoned dev XD
public class PlayerCollision : MonoBehaviour {
public PlayerMovement movement;
void OnCollisionEnter (Collision collisionInfo)
{
if(collisionInfo.collider.tag == "Obstacle")
{
Debug.Log("hit!");
}
}
}
Still didn't work, But thanks for the help anyway!
Answer by insoluzioni · Feb 19, 2021 at 01:09 AM
It looks like your code is fine, it must be something with your GameObjects. For collision to be triggered, both GameObjects (the Player and Obstacle) should have RigidBodies attached to them, not sure but I think both of them need a Collider too.
Also, I would put the Debug.Log outside the If statement. If it works, it means your haven't set the correct tag name.
Take a look at Unity documentation, they're very helpful: https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
This is incorrect. At least one object (the one with this script attached) must have a rigidbody, both must have a collider, both colliders must not be triggers.
This makes sense when you think about it as the colliders tell the physics engine where the objects are, so there is no way to have collision without two of them, and collisions have the effect of stopping moving objects, so at least one needs to be a rigidbody (an object that can move with physics). It doesn't have to be both though, because moving objects can collide with stationary ones.
Answer by Retsof · Feb 19, 2021 at 02:45 PM
I'm gonna go ahead and mark this as solved. I fixed half of the problem and the part that's left (movement.enbled) is unrelated to this question. Thanks to everyone that helped!
I forgot to add movement.enabled = false;
...I think I'm gonna be good at this.
Your answer
Follow this Question
Related Questions
Hi! How can I use the OnTriggerEnter2D function for the game objects that I instantiate? 3 Answers
GameObjects glitching through other Objects when being held 0 Answers
How can i rotate a transform using lerp but keep the nose in same direction/angle on x ? 1 Answer
Replacing mesh collider with primitive colliders via script 1 Answer