- Home /
Why counter increases with any collision?
I am making a ping pong game, and i made a counter for each the player and the AI player. I attached a collider to the ball to detect which border it hits the increment the counter, but what happens that when the ball hit the top border it increments both counters, but the counter for down counter is going right. So i decided to attach the collision detection script to the borders not to the ball, but the same problem existing, here the code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Text; using System.IO;
public class AIScore : MonoBehaviour {
private int AI_Counter =0;
private int Player_Counter=0;
public Text AI_Counter1;
public Text Player_Counter1;
CircleCollider2D circleCollider;
// Use this for initialization
void Start () {
circleCollider = GetComponent<CircleCollider2D> ();
AI_Counter1.text = "Counter: " + AI_Counter.ToString ();
}
void OnCollisionEnter2D (Collision2D col) {
if (col.gameObject.tag=="Ball") {
AI_Counter++;
AI_Counter1.text = "Counter: " + AI_Counter.ToString ();
}
}
}
Probably you have to add more information, some images of the Scene GameObject names, and how did you create your borders. That will help to understand what's going on. The Script seems to work fine.
The problem might be in where are you adding this script, or how did you parent the borders or even the ball.
Sad that I can't help, but if you update with some more info, I will try and help :)
Answer by m0mohy92 · Jun 28, 2019 at 03:26 AM
Unfortunately, an error saying "error parsing file" appears every time i try to upload an image, but you can google it by writing pong game, many photos will appear.
At first this script was attached to the ball, with changing tags to borders tags. When it didn't work i attached the above script to the down border to count the first player score.
Your answer
Follow this Question
Related Questions
Scoring/Counting how many specific objects are placed on a specific plane. 1 Answer
Score count increase on hit 2 Answers
Collision with cube isn't working when Instantiate clone it 2 Answers
CPU cost of using multiple rigidbodies 0 Answers
Collision Counter +1 To Counter Only Once When Collided 1 Answer