- Home /
 
help with goal detection system,Goal detection systems not working
I have been trying to make a detection system for the game "pong". But i can't seem to find any way to detect if a goal has been scored I have used tried two scripts both of which don't work The first one is:
        public GameObject Ball; //assigning object
         private float position; 
     
         void Start()
         {
             position = Ball.transform.position.x;
             //gets the pos of x
         }
     
         void FixedUpdate()
         {
             if (position > 20 ||
                 position < -20) //checks if it's out of bounds
             {
                 Debug.Log("Goal!");
             }
         }
 
               The other one is the one i prefer works because it has a scoreboard with it, and i'm still a begginer so i don't know how to use scoreboards.
        private int Player1Score;
         private int Player2Score;
     
         public void Player1Scored()
         {
             Player1Score++;
             Player1Text.GetComponent<TextMeshProUGUI>().text = Player1Score.ToString();
         }
     
         public void Player2Scored()
         {
             Player2Score++;
             Player2Text.GetComponent<TextMeshProUGUI>().text = Player2Score.ToString();
         }
 
 
               Please help me i've been working on this for hours now
Answer by mbro514 · Jul 04, 2020 at 05:48 PM
So, with your first script, the variable "position" is only being set at the beginning of the game. As a result, the variable "position" is never going to change, regardless of where the ball goes. What you need to do is move the line "position = Ball.transform.position.x;" to the beginning of the FixedUpdate method.
As for the second script, everything looks fine, except that (as far as I can tell) you're not calling the Player1Scored() or the Player2Scored() methods when one of the players scores.
Your answer