- Home /
I'm having issues increasing my lives
I was able to work into my code a way to increase lives every multiple of 1500, but now this code is not working and isnt even reaching the if statement. What am I missing?
using UnityEngine; using System.Collections;
public class projectile : MonoBehaviour {
//class level data members
public float ProjectileSpeed;
public GameObject Explosion_p;
private bool LivesIncremented = false;
private Transform myTransform;
// Use this for initialization
void Start()
{
myTransform = transform;
player.ShotsFired++;
}
// Update is called once per frame
void Update()
{
float amtToMove = ProjectileSpeed * Time.deltaTime;
myTransform.Translate(Vector3.up * amtToMove);
if (myTransform.position.y > 5.2f)
{
Destroy(this.gameObject);
player.Score -= 2;
}
}
void OnTriggerEnter(Collider otherObject)
{
if (otherObject.tag == "enemy")
{
enemy Enemy = (enemy)otherObject.gameObject.GetComponent("enemy");
Instantiate(Explosion_p.gameObject, transform.position, transform.rotation);
enemy.MinSpeed += .25f;
enemy.MaxSpeed += .5f;
Enemy.SetPositionAndSpeed();
Destroy(gameObject);
player.Score += 100;
//player.HighScore = player.Score;
if ( player.Score % 1500 == 0 && !LivesIncremented)
{
Debug.Log ("increasing lives");
player.Lives ++;
}
}
}
}
Answer by l3fty · May 05, 2014 at 06:41 PM
On line 44 you are destroying the gameObject, so it probably doesn't get a chance the execute the rest of the code in OnTriggerEnter. Perhaps move that to the bottom of the function.
the destruction of the game objects gives the value for the score. So once this value reaches 1500 lives in turn should go up
ok that worked I put the if statement in before the destruction and it worked
Answer by edragongaming · May 05, 2014 at 08:21 PM
I rearanged the code to where the if statement is before the destroy (GameObject) and it gives me a life straight off when I destroy the first enemy Game Object
void OnTriggerEnter(Collider otherObject)
{
if (otherObject.tag == "enemy")
{
enemy Enemy = (enemy)otherObject.gameObject.GetComponent("enemy");
Instantiate(Explosion_p.gameObject, transform.position, transform.rotation);
enemy.MinSpeed += .25f;
enemy.MaxSpeed += .5f;
Enemy.SetPositionAndSpeed();
if ( player.Score % 1500 == 0 && !LivesIncremented)
{
Debug.Log ("increasing lives");
player.Lives ++;
}
Destroy(gameObject);
player.Score += 100;
//player.HighScore = player.Score;
}
Your answer
Follow this Question
Related Questions
Beginner need some help on Card Game 2 Answers
Simple heart system using new UI features? 0 Answers
When Gui Text Clicked, Quit Game 2 Answers
GUI trouble 1 Answer
Pick up objects 2 Answers