Problem With GameManager Script in Pong
This is my second game. I am having a problem that I have narrowed down to what seems to be with the "gameManager" script. Everything works--when I say everything I mean I can move the paddles and the ball moves and bounces--when I have the background, paddles with playerControls script attached and ball with ballControls script attached. The game will start, the paddles move and the ball moves around the scene as it should. The problem comes in when I add the gameManager script to the HUD. I started removing components until it worked again; that's how I pinned down the GM script. I have been through the code several times. It is exactly as it is in the tutorial. This doesn't make sense, as there are no errors thrown in the console. Can someone please take a look at my code and see if they see something I am missing? I would much appreciate any help.
This is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GameManager : MonoBehaviour {
public static int PlayerScore1 = 0;
public static int PlayerScore2 = 0;
public GUISkin layout;
GameObject theBall;
// Start is called before the first frame update
void Start()
{
theBall = GameObject.FindGameObjectWithTag("Ball");
}
public static void Score (string wallID)
{
if (wallID == "RightWall")
{
PlayerScore1++;
}
else
{
PlayerScore2++;
}
}
void OnGUI ()
{
GUI.skin = layout;
GUI.Label(new Rect(Screen.width / 2 - 150 - 12, 20, 100, 100), "" + PlayerScore1);
GUI.Label(new Rect(Screen.width / 2 + 150 + 12, 20, 100, 100), "" + PlayerScore2);
if (GUI.Button(new Rect(Screen.width / 2 - 60, 35, 120, 53), "RESTART"))
{
PlayerScore1 = 0;
PlayerScore2 = 0;
theBall.SendMessage("RestartGame", 0.5f, SendMessageOptions.RequireReceiver);
}
if (PlayerScore1 == 10)
{
GUI.Label(new Rect(Screen.width / 2 - 150, 200, 2000, 1000), "PLAYER ONE WINS");
theBall.SendMessage("ResetBall", null, SendMessageOptions.RequireReceiver);
}
else if (PlayerScore2 == 10)
GUI.Label(new Rect(Screen.width / 2 - 150, 200, 2000, 1000), "PLAYER TWO WINS");
theBall.SendMessage("ResetBall", null, SendMessageOptions.RequireReceiver);
}
}