-1

I recently started making a Pong game in Unity from this tutorial https://youtu.be/YHSanceczXY and one of the codes isnt working

enter code here 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

    public class Goal : MonoBehaviour
    {
    public bool isPlayer1Goal;

    private void OnTriggerEnter2D(Collider collision)
    {
        if (collision.gameObject.CompareTag("Ball"))
        {
            if (!isPlayer1Goal)
            {
                Debug.Log("Player 2 Scored...");
                GameObject.Find("GameManager").GetComponent<GameManager>().Player2Scored;
            }
            else
            {
                Debug.Log("Player 1 Scored...");
                GameObject.Find("GameManager").GetComponent<GameManager>().Player1Scored;
            }
        }
    }
}
  • Program statements have to _do_ something, i.e. have a clear side-effect. Retrieving `Player2Scored` and `Player1Scored` property values doesn't do anything, it just gets those values which are then immediately discarded. If those are not properties, then perhaps you forgot the parentheses after their names required to _call_ the methods. See duplicates. – Peter Duniho Jul 03 '21 at 14:48

1 Answers1

0

This line of code doesn't do anything:

GameObject.Find("GameManager").GetComponent<GameManager>().Player1Scored;

It resolves to a value, but nothing is done with that value. It's logically equivalent to a line of code like this:

1;

From the usage, it looks like you intended to log that value. For that you'd use Debug.Log() just like you do in the previous line:

Debug.Log("Player 1 Scored...");
Debug.Log(GameObject.Find("GameManager").GetComponent<GameManager>().Player1Scored);

Or perhaps put them on the same line:

Debug.Log($"Player 1 Scored... {GameObject.Find("GameManager").GetComponent<GameManager>().Player1Scored}");
David
  • 208,112
  • 36
  • 198
  • 279