- Home /
How to keep score and reset score when hitting walls?
I have a juggling ball. Every time it's pressed the score goes up by 1. I'm struggling to find a way that if the ball hits the bottom the score resets to 0 and if it hits the sides for the score to keep going. My bottom collider is named diebox. My up, right, and left sides are named deadzone. No matter what it hits I can't seem to get the score running right.
With this code, my score resets to 0 no matter what side it hits.
{ public GameObject deadZone; public GameObject dieBox; public GameObject scoreObject; public Text scoreText; public int score;
void Start()
{
scoreText = scoreObject.GetComponent<Text>();
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<Rigidbody2D>().velocity.y < 0)
{
forceY *= (Mathf.Abs(GetComponent<Rigidbody2D>().velocity.y) / 4);
}
}
else
{
forceY = (transform.position.y - p.y) * 100;
}
GetComponent<Rigidbody2D>().AddForce(new Vector2(forceX, forceY));
}
void OnCollisionEnter2D(Collision2D col)
{
if (dieBox.gameObject.tag == "DieBox")
{
score = 0;
scoreText.text = " " + score;
}
}
}
Answer by Tehemus · May 09, 2020 at 05:18 PM
I think dieBox.gameObject.tag == "DieBox"
might always be True, try col.gameObject.tag == "DieBox"
instead.
Your answer
Follow this Question
Related Questions
Goal Collision for pong game 0 Answers
OnCollisionEnter Sine Wave to Character 0 Answers
Box goes through walls 2 Answers