OnCollisionEnter2d incoming object is the wrong type
Can someone help me understand what's wrong with my collider2d's? I'm working on a very simple brick breaker game. I have a prefab called BonusItem. When the ball breaks a brick, it will instantiate a random bonus item, and fall at a steady speed. I have a Box Collider2d on the bonus item, and a Capsule Collider2d on the paddle. This part works fine. In the code on both objects, I have this:
private void OnCollisionEnter2D(Collision2D collision) {
Debug.Log("Collision on bonus item: " + collision.gameObject.name);
}
private void OnCollisionEnter2D(Collision2D collision) {
Debug.Log("Collisions on paddle: " + collision.gameObject.name);
}
But in the console it always says:
Collision on bonus item: Ball
UnityEngine.Debug:Log(Object)
BonusItem:OnCollisionEnter2D(Collision2D) (at Assets/Scripts/BonusItem.cs:29)
When the paddle and bonus item collide, they both say the ball is the incoming collider. But the ball is way out on the other side of the level bouncing off a wall. The console message only appears when the bonus item collides with the paddle. I just can't seem to figure out what I'm doing wrong. Here's the code for the 3 objects.
Paddle.cs:
public class Paddle : MonoBehaviour {
//Config Vars
[SerializeField] float screenWidthInUnits = 16f;
[SerializeField] float minX = 1f;
[SerializeField] float maxX = 15f;
// Update is called once per frame
void Update() {
float mousePosInUnits = Input.mousePosition.x / Screen.width * screenWidthInUnits;
Vector2 paddlePos = new Vector2(transform.position.x, transform.position.y);
paddlePos.x = Mathf.Clamp(mousePosInUnits, minX, maxX);
transform.position = paddlePos;
}
private void OnCollisionEnter2D(Collision2D collision) {
Debug.Log("Collisions on paddle: " + collision.gameObject.name);
}
}
BonusItem.cs:
public class BonusItem : MonoBehaviour {
[SerializeField] Sprite[] bonusSpriteArray;
[SerializeField] SpriteRenderer spriteRenderer;
// Start is called before the first frame update
void Start() {
spriteRenderer = GetComponent<SpriteRenderer>();
float rand = Random.Range(0f, 1f);
if (rand > 0.5f) {
spriteRenderer.sprite = bonusSpriteArray[0];
} else {
spriteRenderer.sprite = bonusSpriteArray[1];
}
}
// Update is called once per frame
void Update() {
float moveX = 0.1f;
transform.position = Vector3.MoveTowards(transform.position, new Vector3(transform.position.x,transform.position.y -2, transform.position.z), moveX);
}
private void OnCollisionEnter2D(Collision2D collision) {
Debug.Log("Collision on bonus item: " + collision.gameObject.name);
}
}
Ball.cs:
public class Ball : MonoBehaviour {
// config vars
[SerializeField] Paddle paddle1;
[SerializeField] float xPush = 2f;
[SerializeField] float yPush = 15f;
[SerializeField] float randomFactor = 0.2f;
[SerializeField] AudioClip[] ballSounds;
// ref
private Rigidbody2D rigidBody2d;
// state
Vector2 paddleToBallVector;
private bool hasStarted = false;
AudioSource audioSource;
// Start is called before the first frame update
void Start() {
hasStarted = false;
paddleToBallVector = transform.position - paddle1.transform.position;
audioSource = GetComponent<AudioSource>();
rigidBody2d = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update() {
if (!hasStarted) {
LockBallToPaddle();
LaunchBallOnMouseClick();
}
}
private void LaunchBallOnMouseClick() {
if (Input.GetMouseButtonDown(0)) {
hasStarted = true;
rigidBody2d.velocity = new Vector2(xPush, yPush);
}
}
void LockBallToPaddle() {
Vector2 paddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
transform.position = paddlePos + paddleToBallVector;
}
private void OnCollisionEnter2D(Collision2D collision) {
Vector2 velocityAdd = new Vector2(UnityEngine.Random.Range(0f, randomFactor),
UnityEngine.Random.Range(0f, randomFactor));
if (hasStarted) {
AudioClip clip = ballSounds[UnityEngine.Random.Range(0, ballSounds.Length)];
audioSource.PlayOneShot(clip);
rigidBody2d.velocity += velocityAdd;
}
}
public bool GameStarted() {
return hasStarted;
}
}
Can anyone see my problem?
Thanks in advance!
Your answer
Follow this Question
Related Questions
NullreferenceException dealing damage to a child of the enemy 0 Answers
Not all paths return a value 1 Answer
crouch attack collider 2d question 1 Answer
Get all the coordinates inside of a Collider Unity 2D 1 Answer
(C#) Bullet Damage , OnCollisionTrigger2D not working,(C#) Bullet Damage, Trigger not working. 2 Answers