Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jhunglow · Jul 06, 2015 at 03:48 AM · gameobjectarray

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

 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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



Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jhunglow · Jul 06, 2015 at 02:44 PM 0
Share

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?

avatar image ransomink · Jul 06, 2015 at 03:04 PM 0
Share

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

avatar image NeverHopeless · Jul 06, 2015 at 03:04 PM 0
Share

Try with 'Collections' ins$$anonymous$$d.Parsing error should now resolve with this new updated code.

avatar image jhunglow · Jul 06, 2015 at 04:17 PM 0
Share

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)

avatar image NeverHopeless · Jul 06, 2015 at 06:26 PM 1
Share

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!

Show more comments
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jhunglow · Jul 06, 2015 at 04:21 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

Checking if a position is occupied in 2D? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges