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 ();
}
}
}