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 trialTomas Salas
Front End Web Development Techdegree Graduate 37,934 PointsERROR, playerMovement is a TYPE but variable is expected ?
don't know why I'm getting this error
using System.Collections; using System.Collections.Generic; using UnityEngine; 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 FollowCamara followCamera;
private float restarDelay = 3f;
private float restartTimer;
// HAS TO DO WITH THIS VARAIBLE PLAYERMOVEMNT.//////////////////////////////////////////// private PlayerMovement playerMovement ; private PlayerHealth playerHealth;
// Use this for initialization
void Start () {
Cursor.visible = false;
PlayerMovement = player.GetComponent<PlayerMovement> ();
PlayerHealth = player.GetComponent<PlayerHealth> ();
// prevent th epalyer from moving at the sart of the game
playerMovement.enabled = false;
birdMovement.enabled = false;
followCamera.enabled = false;
}
// Update is called once per frame
void Update () {
// if game not stared and keysAPCE IS PRESS UP
if (gameStarted == false && Input.GetKeyUp (KeyCode.Space)) {
StartGame();
}
if (playerHealth.alive == false) {
// end the game...
EndGame();
// incremt a timer p trestaring
restartTimer = restartTimer + Time.deltaTime;
// .. and if reaches the restat delay...
if (restartTimer >= restarDelay) {
//...then reload the currently loaded level.
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
}
private void StartGame() {
gameStarted = true;
gameStateText.color = Color.clear;
playerMovement.enabled = true;
birdMovement.enabled = true;
followCamera.enabled = true;
}
private void EndGame() {
gameStarted = false;
gameStateText.color = Color.white;
gameStateText.text = "Game Over!";
player.SetActive (false);
}
}
3 Answers
Ignacio Uriz
5,042 PointsYou have some problems your code should look like this:
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 = player.GetComponent<PlayerMovement>();
playerHealth = player.GetComponent<PlayerHealth>();
playerMovement.enabled = false;
birdMovement.enabled = false;
followCamera.enabled = false;
}
// Update is called once per frame
void Update () {
if (gameStarted == false && Input.GetKeyUp (KeyCode.Space))
{
StartGame();
}
if (playerHealth.alive == false)
{
EndGame();
restartTimer = restartTimer + Time.deltaTime;
if (restartTimer >= restartDelay)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
}
private void StartGame() {
gameStarted = true;
gameStateText.color = Color.clear;
playerMovement.enabled = true;
birdMovement.enabled = true;
followCamera.enabled = true; //This line is missing entirely from your code
}
private void EndGame()
{
gameStarted = false;
gameStateText.color = Color.white;
gameStateText.text = "Game Over!";
player.SetActive(false);
}
}
Juan Rivera
1,850 PointsWhen you are assigning the PlayerMovement and the PlayerHealth components to the playerMovement and playerHealth variables in the Start () method, you are capitalizing the variables.
Tomas Salas
Front End Web Development Techdegree Graduate 37,934 Pointsmm I see thx, Juan and ignacio