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 trialDavid Reed
Courses Plus Student 306 PointsCan I set the game to end after score = 20 and to display gameStateText.text = "Congrats You Win!" ?
I have been looking at both the Game State script and the Score Counter script as I feel my resolution will involve these 2 scripts. I really would like to figure this out myself, but I am up for a little nudge in the right direction. I really appreciate the thoroughness of this training and after my trial is over I look forward to becoming a customer.
Thank you so much,
Dave
2 Answers
Jaroslav Vankat
12,054 PointsHi,
of course you can. In the update method you can check wether the score is higher than 20 and if it is then you can easily follow the steps from the next video (I assume, didn't see that) to end the game.
So the code would look like that:
void Update () {
if(ScoreCounter.score>=20)
gameStateText.text = "Congrats You Win!";
}
This is obviously not the best approach, because this condition is checked every frame, but for a game like this it should be ok (like the score counter) - but it is possible that I'm wrong here, maybe Nick could leave some information :).
Edit: So after watching the video I'm posting my solution. If you have some questions, feel free to comment.
void Update () {
if(!gameStarted && Input.GetKeyUp(KeyCode.Space)){
StartGame();
}
if(!playerHealth.alive){
EndGame("Game Over");
RestartTheGameWithDelay(); //I've extracted the part with the timer because I'm using it twice
}
if (ScoreCounter.score >= 20) {
EndGame("You Won");
RestartTheGameWithDelay();
}
}
private void RestartTheGameWithDelay(){
//Restarts the game after specified amount of time
restartTimer += Time.deltaTime;
if (restartTimer >= restartDelay) {
Application.LoadLevel(Application.loadedLevel);
}
}
private void StartGame(){
/* same as Nick's */
}
private void EndGame(string message){
gameStarted = false;
gameStateText.color = Color.white;
gameStateText.text = message;
if(message.Equals("Game Over")){
player.SetActive (false);
}
else{
/* we don't destroy the player because he was not killed by the bird */
playerMovement.enabled=false;
birdMovement.enabled=false;
Animator playerAnimator = player.GetComponent<Animator>();
Animator birdAnimator = bird.GetComponent<Animator>();
playerAnimator.SetFloat("Speed",0f); //Stop the movement of the player
birdAnimator.SetFloat("Speed",0f); //Stop the movement of the bird - this doesn't work for me right now and don't know why and it's too late to figure out
}
}
dasgk
2,474 PointsAs a newbie in C# and Unity i've spent around 30 mins for a solution, but it works. Actually, it is required a little bit of code added in Update method, and also another [Serializefield].
Not sure, though, if it still be any helpful
/* this was done to have a way to destroy the bird from the game after the condition is successful.
Drag the "Bird" object from Hierarchy in the "Game Manager" into the new field called "Bird"*/
[SerializeField]
private GameObject bird;
//
// ...here's a bunch of code from video...
//
void Update () {
/* same as Nick's */
if (gameStarted == false && Input.GetKeyUp(KeyCode.Space)) {
StartGame();
}
/* same as Nick's */
if (playerHealth.alive == false) {
EndGame();
RestartTheGameWithDelay ();
}
/* After the frog pick 10 flies, the game stops, player recieves "You've reached the goal!" message,
frog and bird dissapears from the screen */
if (ScoreCounter.score >=10) {
/* Stops the game and shows the message to the player */
gameStarted = false;
gameStateText.color = Color.white;
gameStateText.text = "You've reached the goal!";
/* disabled movements for freezing */
playerMovement.enabled = false;
birdMovement.enabled = false;
followCamera.enabled = false;
/* destroys objects from the game after the goal is reached */
player.SetActive (false);
bird.SetActive (false);
/* see the function from above message. Quite useful :) */
RestartTheGameWithDelay ();
}
}