how to stop at a certain line of code??
Hi i am new to this, i am currently trying to make a 2d shooter game the problem i am getting is i have some enemy tanks that shoot the player but when they are destroyed the code breaks as it is still trying to use them in the shooting code. so i want to add in an if statement that says if it is destroyed dont run the shooting line of code but i dont really know how to do it any help would be appreciated. HERE IS MY CODE using System.Collections; using System.Collections.Generic; using UnityEngine;
public class destroyBullet : MonoBehaviour {
// Use this for initialization
public bool byTime;
public bool byContact;
public float destroyTime;
float bulletSpeed = 0.01f;
float damagePerHit = 1;
public GameObject enemy;
public Transform player;
public Transform tank;
public Transform tank01;
private playerHealth health;
void Start(){
//player script for health
health = GameObject.Find ("trumPlayer").GetComponent<playerHealth> ();
Destroy (this.gameObject, destroyTime);
enemy = GameObject.FindGameObjectWithTag("Player");
player = GameObject.Find ("trumPlayer").transform;
//find the enemy that shoots
tank = GameObject.Find ("tank").transform;
tank01 = GameObject.Find ("tank01").transform;
}
void Update() {
transform.LookAt(enemy.transform);
{
if (GameObject.Find ("tank") == null)
{
// this is where i want to stop it
}
else if(tank.position.x - player.position.x >0){
transform.position = new Vector3 (transform.right.x + bulletSpeed, transform.right.y, transform.right.z);
print("attack Enemy");
}
else if (tank.position.x - player.position.x < 0) {
transform.position = new Vector3 (-transform.right.x + bulletSpeed, transform.right.y, transform.right.z);
}
if(tank01.position.x - player.position.x >0){
transform.position = new Vector3 (transform.right.x + bulletSpeed, transform.right.y, transform.right.z);
print("attack Enemy");
}
if (tank01.position.x - player.position.x < 0) {
transform.position = new Vector3 (-transform.right.x + bulletSpeed, transform.right.y, transform.right.z);
}
}
}
void OnTriggerEnter2D()
{
Destroy (this.gameObject);
health.AlterHealth (-1 );
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612145056im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make fast a coroutine 1 Answer
Compiler error 0 Answers
Jittery background when implementing parallax effect 1 Answer
Problem getting components in loaded scene 0 Answers
Instantiating A group of platform and moving them down 0 Answers