When the Pong Cube hits the “Goal” wall it should add a point to the proper side. However I keep getting “NullReferenceException: Object reference not set to an instance of an object Goal.GetPoint () (at Assets/Scripts/Goal.cs:12)”
Here is my code :
Ball
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour
{
//The speed the ball starts with
public float StartSpeed = 5f;
//The maximum speed of the ball
public float MaxSpeed = 20f;
//How much faster the ball gets with each bounce
public float SpeedIncrease = 0.25f;
//Current speed of the ball
public float currentSpeed;
//Current direction of the ball
public Vector2 currentDir;
//Whether or not the ball is resetting
private bool resetting = false;
public Goal goal;
void Start ()
{
//initialize starting speed
currentSpeed = StartSpeed;
//initialize starting direction
currentDir = Random.insideUnitCircle.normalized;
}
void Update ()
{
//don't move the ball if it's resetting
if (resetting)
return;
//move the ball in the current direction
Vector2 moveDir = currentDir * currentSpeed * Time.deltaTime;
transform.Translate(new Vector3(moveDir.x, 0f, moveDir.y));
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Boundry")
{
//vertical boundary, reverse y direction
currentDir.y *= -1;
}
else if(other.tag == "Player")
{
//Player paddle, reverse x direction
currentDir.x *= -1;
}
else if(other.tag == "Goal")
{
//reset the ball
StartCoroutine(resetBall());
//inform goal of the score
Debug.Log ("before other message sent");
other.SendMessage("GetPoint", SendMessageOptions.DontRequireReceiver);
Debug.Log ("After other message sent");
}
//increase speed
currentSpeed += SpeedIncrease;
//clamp speed to maximum
currentSpeed = Mathf.Clamp (currentSpeed, StartSpeed, MaxSpeed);
}
IEnumerator resetBall()
{
//Reset position, speed and direction
resetting = true;
transform.position = Vector3.zero;
currentDir = Vector3.zero;
currentSpeed = 0f;
//wait for 3 seconds before starting the round
yield return new WaitForSeconds (3f);
Start ();
resetting = false;
}
}
Goal
using UnityEngine;
using System.Collections;
public class Goal : MonoBehaviour
{
// the player who gets a point for this goal, 1 or 2
public int Player = 2;
// the Scorekeeper
public Scorekeeper scorekeeper;
public void GetPoint()
{
// when the ball collides with this goal, give the player a point
scorekeeper.AddScore(Player);
}
}
Scorekeeper
using UnityEngine;
using System.Collections;
public class Scorekeeper : MonoBehaviour
{
// the maximum score a player can reach
public int ScoreLimit = 10;
// the display test for player 1's score
public TextMesh Player1ScoreDisplay;
// the display text for player 2's score
public TextMesh Player2ScoreDisplay;
// Player 1's score
private int p1Score = 0;
// Player 2's score
private int p2Score = 0;
// give the appropriate player a point
public void AddScore( int player )
{
// player 1
if( player == 1 )
{
p1Score++;
}
// player 2
else if( player == 2 )
{
p2Score++;
}
// check if either player reached the score limit
if( p1Score >= ScoreLimit || p2Score >= ScoreLimit )
{
// player 1 has a better score than player 2
if( p1Score > p2Score )
Debug.Log( "Player 1 wins" );
// player 2 has a better score than player 1
if( p2Score > p1Score )
Debug.Log( "Player 2 wins" );
// both players have the same score - tie
else
Debug.Log( "Players are tied" );
// reset scores and start over
p1Score = 0;
p2Score = 0;
}
// display each player's score
Player1ScoreDisplay.text = p1Score.ToString();
Player2ScoreDisplay.text = p2Score.ToString();
}
}