ISSUE DESCRIPTION:
I am working on making an Angry Birds style game. Currently, I am using a GameManager to keep track of the number of balls available. I check this in my “Hook” script which basically deals with Instantiating a new ball if it’s possible. When I try accessing the instance of the game manager (Which is attached to a game object) in the update method everything is fine, but if I try instantiating it in a custom method I get a “Null Reference Error”.
FIXES I HAVE TRIED:
I tried changing the Script Execution order so that the GameManager Script runs first but this didn’t seem to help.
CODE:
Game Manager:
public class GameController : MonoBehaviour {
public static GameController instance = null;
void Awake()
{
if(instance == null)
{
instance = this;
}
else if (instance != this)
{
Destroy(this);
}
DontDestroyOnLoad(gameObject);
}
//How many balls are available
public int numberOfBalls = 3;
//How much gold does the player have
public int gold = 0;
//What level is the player on
public int level = 0;
}
Hook Class:
public class Hook : MonoBehaviour {
//Reference to the ball prefab
public GameObject ballPrefab;
//Is there currently a ball in play
private bool isBallActive;
//The current ball in play
private GameObject currentBall;
//Reference to the Game camera
[SerializeField]private Camera gameCamera;
//On awake assign a new ball to the hook
void Awake()
{
//Create a new ball
createNewBall();
}
//Called every frame
public void Update()
{
//If the currentBall has been destroyed
if(currentBall == null)
{
//No ball is active
isBallActive = false;
}
//If no ball is active, and we have more balls
if (!isBallActive && GameController.instance.numberOfBalls > 0)
{
//Create a new ball
createNewBall();
//Reduce the number of balls left
//GameController.instance.numberOfBalls -= 1;
} else if(!isBallActive && GameController.instance.numberOfBalls <0)
{
Debug.Log("Out of BALLZ");
}
}
//Create a new ball and attach it to the hook
private void createNewBall()
{
//Instantiate a new ball
currentBall = Instantiate(ballPrefab, transform.position, Quaternion.identity);
//Reduce the number of balls left
GameController.instance.numberOfBalls -= 1;
//Make the camera follow new ball
gameCamera.GetComponent<CameraFollow>().playerToFollow = currentBall;
//Get the current rigidbody of the hook
Rigidbody2D rb = GetComponent<Rigidbody2D>();
//Connect it to the ball
currentBall.GetComponent<SpringJoint2D>().connectedBody = rb;
currentBall.GetComponent<Ball>().rb2D_hook = rb;
//Ball is active
isBallActive = true;
}
}