- Home /
The question is answered, right answer was accepted
(Solved) Collision happens, but is not detected?
Hi, I'm trying to make a 2D breakout clone as an assignment, but I've ran into a problem; whenever I start the game the ball will collide with a block, and det block should be destroyed. But it does not. The ball bounces of it and back to the paddle. So I made a debug.log, but it does not show me the collision.
I get the error: MissingComponentException: There is no 'Collider' attached to the "Block" game object, but a script is trying to access it.
However, there is a boxcollider attached to the Block, as well as a rigidbody, so goes for the Ball.
Here is the BoxScript:
using UnityEngine; using System.Collections;
public class BlockScript : MonoBehaviour {
public BlockType blocktype = BlockType.Blue;
private int points;
public int hp;
private int timesHit;
public bool doubleX = false;
public enum BlockType{
Blue = 1,
Green = 5,
Yellow = 10,
Orange = 15,
Red = 20,
Black = -10,
}
void Start(){
}
void update(){
}
void OnCollisionEnter2D(Collision2D col) {
Debug.Log (collider.gameObject.name);
//GameManager.score += points;
Destroy (gameObject);
}
}
Also, changing it between OnCollisionEnter and OnCollisionEnter2D makes the error disappear, but the debug does not detect the collision, nor is the object destroyed.
Answer by tanoshimi · Mar 07, 2015 at 05:05 PM
2D physics and 3D physics are handled by completely different systems and do not interact with each other.
If you're making a 2D game, you need Collider2Ds and Rigidbody2D components on your objects, and use OnCollisionEnter2D() in your code.
They all have Rigidbody 2d and box/circle Collider 2D attached to them. Having OnCollisionEnter2D(Collision2D col) on them rather than void OnCollisionEnter(Collision col) causes the game to freeze and give me an error:
$$anonymous$$issingComponentException: There is no 'Collider' attached to the "Block" game object, but a script is trying to access it.
Because there isn't. Presumably you meant col.gameObject.name, to refer to the col variable passed into the function, not the (apparently non-existant) collider component attached the object the script is on.
Follow this Question
Related Questions
Dying Script error 2 Answers
Distribute terrain in zones 3 Answers
Collision returning an error 0 Answers
Keep getting this error CS8025 1 Answer
Space opening main menu (undesired) 0 Answers