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 trial

Game Development

Vidit Shah
Vidit Shah
6,037 Points

Want to give 3 lifes to player.

I want to Give 3 lifes to the frog so that before it gets eaten up it has 3 chances here is my code i tried but i have failed badly in applying my logic

using UnityEngine;
using System.Collections;

public class PlayerHealth : MonoBehaviour {
    public bool alive;
    [SerializeField]
    private GameObject pickUpPrefab;
    private int life=3;

    // Use this for initialization
    void Start () {
        alive = true;

    }

    // Update is called once per frame
    void OnTriggerEnter (Collider other) {
        for (int i=life; life<3; life--) {

            if (other.CompareTag ("Enemy") && alive == true) {

                alive = false;
                //create pick up particles..

                Instantiate (pickUpPrefab, transform.position, Quaternion.identity);

            }
        }
    }
}

1 Answer

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Hi Vidit Shah,

You're not too far off! This is a really good attempt. The problem in your code is that you're using a loop. This loop will run 3 times any time the player collides with another object, and any code inside of it will be executed.

Instead, you should first look for a collision, then determine whether or not it's an enemy. If it is an enemy, then decrease the life variable by one. Then, while still inside the same 'enemy tag' block, you should check the life variable. If it's less than or equal to zero, then set the alive variable to false and create the pickup particles.

I haven't actually tested this code in the game, but I think this should work and give the player 3 hits before they actually lose the game.

I hope that helps, or at least gets you on the right track!

using UnityEngine;
using System.Collections;

public class PlayerHealth : MonoBehaviour {

  public bool alive;
  [SerializeField]
  private GameObject pickUpPrefab;
  private int life = 3;

  // Use this for initialization
  void Start () {
    alive = true;
  }

  // When the player collides with another object...
  void OnTriggerEnter (Collider other) {

    // If it's an enemy...
    if (other.CompareTag ("Enemy")) {

      // Decrease life by one.
      life--;

      // If the player has 0 life or less...
      if (life <= 0) {
        // ...then set alive to false...
        alive = false;
        // ...and create pick up particles.
        Instantiate (pickUpPrefab, transform.position, Quaternion.identity);
      }

    }
  }
}
Vidit Shah
Vidit Shah
6,037 Points

Thank You so much Sir Nick Pettit it worked totally fine. Thank You :)