Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSean May
9,145 PointsPlayer Movement, Enemy Movement, follow camera not disabled before pressing space
I've been working through the tutorials, and everything has worked thus far, but, now that I've written everything for the GameState script, the player movement and enemy movement as well as the follow camera aren't being disabled upon launch before I press the space button. Here's my code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameState : MonoBehaviour {
private bool gameStarted = false;
[SerializeField]
private Text gameStateText;
[SerializeField]
private GameObject player;
[SerializeField]
private BirdMovement birdMovement;
[SerializeField]
private FollowCamera followCamera;
private float restartDelay = 3f;
private float restartTimer;
private PlayerMovement playerMovement;
private PlayerHealth playerHealth;
// Use this for initialization
void Start () {
Cursor.visible = false;
playerMovement = playerMovement.GetComponent<PlayerMovement>();
playerHealth = playerHealth.GetComponent<PlayerHealth>();
//prevent player, bird from moving at beginning
playerMovement.enabled = false;
birdMovement.enabled = false;
//prevent follow camera from moving
followCamera.enabled = false;
}
private void StartGame() {
//set game state
gameStarted = true;
//remove start text
gameStateText.color = Color.clear;
//enable player, bird movement
playerMovement.enabled = true;
birdMovement.enabled = true;
//enable camera movement
followCamera.enabled = true;
}
private void EndGame() {
//set game state
gameStarted = false;
//re-enable text, change to 'game over'
gameStateText.color = Color.white;
gameStateText.text = "Game Over";
//remove player from game
player.SetActive (false);
}
// Update is called once per frame
void Update () {
//if game is not started, and player presses spacebar, then...
//Input is a built-in class
if(gameStarted == false && Input.GetKeyUp(KeyCode.Space)) {
//start the game
StartGame();
}
//if player is no longer alive...
if(playerHealth.playerAlive == false) {
//...end the game
EndGame();
//after game ends, start timer to count to restart
restartTimer = restartTimer + Time.deltaTime;
if(restartTimer > restartDelay){
//reload the current level
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
}
}
Sometimes when I run the debugger I get the following error
NullReferenceException: Object reference not set to an instance of an object
on the
playerMovement = playerMovement.GetComponent<PlayerMovement>();
line. It's so upsetting to get this error after going through so much going right :D, so any help would be very much appreciated.
Sean May
9,145 PointsSean May
9,145 PointsFigured this out with some digging!
the
playerMovement = playerMovement.GetComponent<PlayerMovement>();
should be
playerMovement = player.GetComponent<PlayerMovement>();