How to remove a gameobject from an array

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 ();
	}

}

To properly format your code use the 101 010 button. For more information on how to effectively use Unity Answers watch the Unity Answers tutorial https://www.youtube.com/watch?v=ezAPpViLs2Q and read the faq: http://answers.unity3d.com/page/faq.html.

2 Answers

2

Looks like you’re using Built-in Arrays; you cannot resize them, so attempting to remove an item from the array will not work.

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 ();
	}
}

I get this error when i type zombie = new System.Collection.Generic.List(); Error: error CS0234: The type or namespace name Collection' does not exist in the namespace System'. Are you missing an assembly reference? and are the if(zombie.Contains(col.gameObject) if statements suppose to be within the other if statements?

At the top of your script make sure you have: using System.Collections.Generic; Then, when declaring a list, use this: public List<GameObject> zombie; Make sure to instantiate the list before using it, either when declaring it or in the Awake() or Start() functions: zombie = new List<GameObject>();

Try with 'Collections' instead.Parsing error should now resolve with this new updated code.

The if(zombie.Contains(col.gameObject)) creates a lot of parsing errors but all of the brackets are in the right place. Im not sure why i am getting all these errors. Errors Unexpected symbol }' (line 89) void' cannot be used in this context (for void SetCountText) Unexpected symbol (', expecting )', ,', ;', [', or =' (line 119) error CS8025: Parsing error(line 129) error CS1525: Unexpected symbol }' (line 89) error CS1525: Unexpected symbol }' (line 97)

Ok if you are getting parsing error then don't use my given code since it is partial piece of your code with modifications. Use your own code and add the following changes: Add these lines: if(zombie.Contains(col.gameObject)) { zombie.Remove(col.gameObject); } in your OnCollisionEnter and OnTriggerEnter, just above the line: Destroy(xxxx.gameObject); Does it removes parsing error then ?