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 trialSindre Berge
Courses Plus Student 3,109 PointsMy frog wont die
Hi, i have gone over this course a couple of times now and i can't figure out why my frog wont die.
here are my GameState.cs
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 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> ();
// Prevent the player from moving at the start of the game
playerMovement.enabled = false;
// Prevent the bird from mobing at the start of the game
birdMovement.enabled = false;
// Prevent the follow camera from moving from its game position
followCamera.enabled = false;
}
// Update is called once per frame
void Update () {
// If the game is not startet and the player presses the spacebar...
if (gameStarted == false && Input.GetKeyUp (KeyCode.Space)) {
// ...then start the game.
StartGame ();
// If the player is no longer alive then...
if (playerHealth.alive == false) {
//...end the game...
EndGame ();
//...increment a timer to count up to restarting...
restartTimer = restartTimer + Time.deltaTime;
//... and if it reaches the restart delay...
if (restartTimer >= restartDelay) {
// ...then reload the currently loaded scene.
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
}
}
private void StartGame() {
// set game state
gameStarted = true;
// Removing the start text
gameStateText.color = Color.clear;
// Allow the player to move
playerMovement.enabled = true;
// Allow the bird to move
birdMovement.enabled = true;
// Allow the camera to move
followCamera.enabled = true;
}
private void EndGame () {
// set the game state
gameStarted = false;
// show the game over text
gameStateText.color = Color.white;
gameStateText.text = "Game Over!";
// Remove the player from the game
player.SetActive (false);
}
}
and in my Game Manager in unity i have:
Fly Spawner (Script) script : FlySpawner.cs Fly Prefab: Fly Total Fly Mininum: 10
Game State (Script) Script: GameState.cs Game State Text (Text) Player: player Bird Movement: Bird (BirdMovement) Follow Camera: Main Camera (FollowCamera)
if you want to look at my other script files from the course you can find them on github: https://github.com/trust96/Learning-To-Make-Games-With-Unity
2 Answers
Steven Parker
231,275 PointsIt could be your Update routine logic.
I notice that if the game has not started yet, the Update routine will start it and then check if the frog is still alive. But wouldn't you always want to check if the frog is still alive?
I'm guessing that first if statement should only enclose the StartGame line, and the next one should be a peer and not nested inside.
Jonathan Bernard
525 PointsThank you that helped me out, I was so stumped I watched the video a few times to see where the {} were, he is correct as I had the same issue and this fixed it.
Sindre Berge
Courses Plus Student 3,109 PointsSindre Berge
Courses Plus Student 3,109 Pointsim sorry but i dont quite understand what you mean :/
Steven Parker
231,275 PointsSteven Parker
231,275 PointsMaybe this will make it clear. So in the Update function:
Sindre Berge
Courses Plus Student 3,109 PointsSindre Berge
Courses Plus Student 3,109 PointsOMG yes thank you that did it, but just so i get it right in my head :P
so beacouse it was nestet inside
it did only check once becouse the game already had started?
Steven Parker
231,275 PointsSteven Parker
231,275 PointsWhen it was nested, it only checked when the game first started, and it never checked again during the game.
Remember to delete the duplicate question.