- Home /
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 ();
}
}
Answer by NeverHopeless · Jul 06, 2015 at 05:08 AM
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;
$$anonymous$$ake 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' ins$$anonymous$$d.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)
I guess you cannot take action over the collision of bullet and zombie inside player behaviour because OnCollisionEnter
functions calls when current gameObject collides with other (passes in parameter) gameObject. The best you can do is to write a script for BulletBehaviour and add this function:
void OnCollisionEnter (Collider other)
{
if (other.gameObject.tag == ("zombie")) {
// Find the player game object and FPS_Health script and send a message to remove zombie.
GameObject.FindGameObjectWihtTag("Player").GetComponent<FPS_Health>().RemoveZombie(other.gameObject);
}
}
and in player's "FPS_Health" script create a function like:
public void RemoveZombie(GameObject targettedZombie)
{
if (zombie.Contains (targettedZombie)) {
zombie.Remove (targettedZombie);
}
}
Now remove zombie.Contains
and zombie.Remove
code everywhere else, since this function RemoveZombie
would be enough to handle this.
I can't write the actual code since i can not compile it. Please try to get the idea and then try to implement it. If you stuck somewhere please discuss then. Hope it helps!
Answer by ransomink · Jul 06, 2015 at 02:56 PM
Looks like you're using Built-in Arrays; you cannot resize them, so attempting to remove an item from the array will not work.
Here is the code :
using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic;
public class FPS_Health : $$anonymous$$onoBehaviour { 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"));
//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.Get$$anonymous$$ey ($$anonymous$$eyCode.Escape)) {
Application.LoadLevel ("$$anonymous$$enu");
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.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 (zombie.Contains (col.gameObject)) {
zombie.Remove(col.gameObject)
}
}
if (col.gameObject.name == "Health") {
count = count + 10;
SetCountText();
Destroy(col.gameObject);
if (zombie.Contains (col.gameObject)) {
zombie.Remove(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 ();
}
}
Your answer
Follow this Question
Related Questions
2D Array of GameObjects... 1 Answer
HELP Find gameObject With tag in another array 1 Answer
Creating an array of prefabs? 4 Answers
Enabled all gameobjects in array 1 Answer