I am trying to remove the zombie gameobject once it has been destroyed from the array. I am not sure how to do this. The array is located on the player object but needs to check to see if one of the gameobject in the array has been destroyed and if it has then remove it from the array. I just don’t know what the code looks like.
Here is my code on what i have so far:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class FPS_Health : MonoBehaviour {
public int count;
public Text health;
public int ammo_count;
public Text ammo;
public GameObject bullet;
public float dist;
public float time;
public GameObject ammo_weapon;
private AudioSource reload;
public AudioClip reload_weapon;
Transform enemy;
public GameObject player_reload;
public GameObject[] zombie;//The array of zombies (enemys)
public void Start()
{
reload = player_reload.GetComponent<AudioSource> ();
zombie = GameObject.FindGameObjectsWithTag("zombie");
time = 2;
ammo_count = 100;
count = 100;
}
public void Update()
{
foreach (GameObject zombies in zombie) {
dist = Vector3.Distance(zombies.transform.position, transform.position);//only calculates the distance of one of the enemys not all of them
if (dist <= 2) {
time -= 1 * Time.deltaTime;
}
if (time <= 0) {
count = count - 10;
SetCountText();
time = 2;
}
}
if (Input.GetKey (KeyCode.Escape)) {
Application.LoadLevel ("Menu");
}
if (Input.GetKey (KeyCode.R)) {
Application.LoadLevel(Application.loadedLevel);
}
if (count <= 0) {
Application.LoadLevel (Application.loadedLevel);
} else if (count >= 100) {
count = 100;
}
if(Input.GetButtonDown("Fire1"))
{
ammo_count = ammo_count - 1;
SetCountText();
}
if (ammo_count < 0) {
ammo_count = 0;
bullet.SetActive (false);
} else if (ammo_count > 0) {
bullet.SetActive(true);
}
}
void OnCollisionEnter (Collision col)
{
if (col.gameObject.name == "zombie_normal 1 3") {
count = count - 10;
SetCountText();
}
if (col.gameObject.name == "ammo") {
ammo_count = ammo_count + 25;
SetCountText();
Destroy(col.gameObject);
reload.Play ();
}
if (col.gameObject.name == "Health") {
count = count + 10;
SetCountText();
Destroy(col.gameObject);
}
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == ("ammo"))
{
ammo_count = ammo_count + 25;
SetCountText();
Destroy(other.gameObject);
}
}
void SetCountText ()
{
health.text = "Health: " + count.ToString ();
ammo.text = "Ammo: " + ammo_count.ToString ();
}
}
You should change your GameObject[]
to List<GameObject>
, using this way you have couple of functions to manipulate array.
void OnCollisionEnter (Collision col)
{
if (col.gameObject.name == "zombie_normal 1 3") {
count = count - 10;
SetCountText();
}
if (col.gameObject.name == "ammo") {
ammo_count = ammo_count + 25;
SetCountText();
if(zombie.Contains(col.gameObject)) { zombie.Remove(col.gameObject); } // Here removed from list
Destroy(col.gameObject);
reload.Play ();
}
if (col.gameObject.name == "Health") {
count = count + 10;
SetCountText();
if(zombie.Contains(col.gameObject)) { zombie.Remove(col.gameObject); } // Here removed from list
Destroy(col.gameObject);
}
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == ("ammo"))
{
ammo_count = ammo_count + 25;
SetCountText();
if(zombie.Contains(col.gameObject)) { zombie.Remove(col.gameObject); } // Here removed from list
Destroy(other.gameObject);
}
}
You also need to change this in the start when update to List
:
zombie = new System.Collection.Generic.List<GameObject>();
zombie.AddRange(GameObject.FindGameObjectsWithTag("zombie"));
Hope it helps!
EDIT:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class FPS_Health : MonoBehaviour
{
public int count;
public Text health;
public int ammo_count;
public Text ammo;
public GameObject bullet;
public float dist;
public float time;
public GameObject ammo_weapon;
private AudioSource reload;
public AudioClip reload_weapon;
Transform enemy;
public GameObject player_reload;
public List<GameObject> zombie;//The array of zombies (enemys)
public void Start ()
{
reload = player_reload.GetComponent<AudioSource> ();
zombie = new List<GameObject> ();
zombie.AddRange (GameObject.FindGameObjectsWithTag ("zombie"));
time = 2;
ammo_count = 100;
count = 100;
}
public void Update ()
{
foreach (GameObject zombies in zombie) {
dist = Vector3.Distance (zombies.transform.position, transform.position);//only calculates the distance of one of the enemys not all of them
if (dist <= 2) {
time -= 1 * Time.deltaTime;
}
if (time <= 0) {
count = count - 10;
SetCountText ();
time = 2;
}
}
if (Input.GetKey (KeyCode.Escape)) {
Application.LoadLevel ("Menu");
}
if (Input.GetKey (KeyCode.R)) {
Application.LoadLevel (Application.loadedLevel);
}
if (count <= 0) {
Application.LoadLevel (Application.loadedLevel);
} else if (count >= 100) {
count = 100;
}
if (Input.GetButtonDown ("Fire1")) {
ammo_count = ammo_count - 1;
SetCountText ();
}
if (ammo_count < 0) {
ammo_count = 0;
bullet.SetActive (false);
} else if (ammo_count > 0) {
bullet.SetActive (true);
}
}
void OnCollisionEnter (Collision col)
{
if (col.gameObject.name == "zombie_normal 1 3") {
count = count - 10;
SetCountText ();
}
if (col.gameObject.name == "ammo") {
ammo_count = ammo_count + 25;
SetCountText ();
if (zombie.Contains (col.gameObject)) {
zombie.Remove (col.gameObject);
}
Destroy (col.gameObject);
reload.Play ();
}
if (col.gameObject.name == "Health") {
count = count + 10;
SetCountText ();
if (zombie.Contains (col.gameObject)) {
zombie.Remove (col.gameObject);
}
Destroy (col.gameObject);
}
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == ("ammo")) {
ammo_count = ammo_count + 25;
SetCountText ();
if (zombie.Contains (other.gameObject)) {
zombie.Remove (other.gameObject);
}
Destroy (other.gameObject);
}
}
void SetCountText ()
{
health.text = "Health: " + count.ToString ();
ammo.text = "Ammo: " + ammo_count.ToString ();
}
}
Looks like you’re using Built-in Arrays; you cannot resize them, so attempting to remove an item from the array will not work.