Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

How to fix lives counter in C#

$
0
0
so with in my project there is an object that lowers the score by ten and the lives by one but every time this collision occurs the score automatically goes to zero please help the entire code is as follows using UnityEngine; using System.Collections; public class PlayerController : MonoBehaviour { public float speed; public GUIText countText; public GUIText winText; public GUIText livesText; private int lives; private int count; private Transform cameraTransform; void start () { count = 0; SetCountText (); lives = 10; SetLivesText (); winText.text = ""; } void FixedUpdate () { //The ball will move horizontal when pressing the side arrow keys float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); //When the ball is moving "horizontal" it is moving on the x axis, when the ball is moving "vertical" it's moving on the Z axis Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); //The ball which is a rigidbody object will be moved when a certain amount of force is added to it rigidbody.AddForce (movement * speed * Time.deltaTime); } void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "PickUp") { //Every time the ball collides with a cube, the counter will add one other.gameObject.SetActive(false); count = count + 1; SetCountText (); } if (other.gameObject.tag == "Capsule") { //Every time the ball collides with a capsule, the counter will add two other.gameObject.SetActive(false); count = count + 2; SetCountText (); } if (other.gameObject.tag == "diamond") { //Every time the ball collides with a diamond, the counter will add five other.gameObject.SetActive(false); count = count + 5; SetCountText (); } if (other.gameObject.tag == "Enemy") { //Every time the ball collides with a icosphere, the counter will subtract one life and 10 points other.gameObject.SetActive(false); lives = lives + -1; SetLivesText (); count = count + -10; SetCountText (); } } void SetLivesText (){ livesText.text = "lives:" + count.ToString (); if (lives <= -1) { winText.text = "GameOver"; livesText.text = "Lives: 0"; if (audio.isPlaying) { audio.Stop (); } } } void SetCountText () { //When the counter reaches fourty, the "you win" message will pop up countText.text = "Count: " + count.ToString (); if(count >= 40) { winText.text = "YOU WIN!"; if (audio.isPlaying){ audio.Stop(); } } } } the main issue lies with the Enemy game object and the lives counter

Viewing all articles
Browse latest Browse all 1488

Trending Articles