void OnCollisionEnter2D(Collision2D col)
{
if (dieBox.gameObject.tag == “DieBox”)
{
scoreText.text = " " + score;
score = 0;
}
}
This is the code I have so when the ball collides with the diebox object(bottom) it should reset the score to 0.
My left, right, and up side colliders are named deadzone. For some reason it still resets the score if it collides with the other 3 sides.
I’m having trouble figuring out what I need to put in order for the score to stay as is no matter what 3 sides it hits until it comes in contact with the bottom and then have it reset to 0.
First, which object is this script attached to? The Ball? The box? Hopefully the ball.
Where did the variable “dieBox” come from? It looks to me like you’re ignoring what you actually collided with and instead looking at the tag of this “dieBox” object.
If you want to check the tag of the object you actually collided with you can get that from the Collision2D parameter of your OnCollisionEnter2D method:
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.CompareTag("DieBox") {
Debug.Log("We hit the die box!");
}
}
okay so how can I fix this?
{
public GameObject deadZone;
public GameObject dieBox;
public GameObject scoreObject;
public Text scoreText;
public int score;
void Start()
{
scoreText = scoreObject.GetComponent();
score = 0;
}
void OnMouseDown()
{
score++;
scoreText.text = " " + score;
Vector3 p = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float forceX;
forceX = (transform.position.x - p.x) * 500;
float forceY;
if ((transform.position.y - (p.y - 1.5f)) > 0)
{
forceY = (transform.position.y - (p.y - 1.5f)) * 300;
if (GetComponent().velocity.y < 0)
{
forceY *= (Mathf.Abs(GetComponent().velocity.y) / 4);
}
}
else
{
forceY = (transform.position.y - p.y) * 100;
}
GetComponent().AddForce(new Vector2(forceX, forceY));
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.CompareTag(“DieBox”))
{
}
}
}
How can I make the ball keep the score when it hits my left, right, and up sides which I call the deadzone?
How can I me the score reset to zero once it hits the bottom called my diebox?