Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
This question was closed Jan 26, 2019 at 11:36 AM by Vendrict for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Vendrict · Jan 26, 2019 at 10:17 AM · destroyrigidbody2dprefabsboxcollider2darraylist

How do I destroy a prefab from an array list?

Hi!

These are my declarations:

 GameObject player, HitObject, pacmanLives, lifeClone, orangeGhost, redGhost, pinkGhost, blueGhost;
 
 Vector2 firstLife = new Vector2(-17f, -13f);
 
 public List<GameObject> Occurrences = new List<GameObject>();
 
 public bool canKillGhost = false;

 void Start () {
         FindObjects();
         SpawnLives();
     }
 
 void FindObjects () {
         Player = GameObject.Find("Player");
         pacmanLives = Resources.Load("Prefabs/PacManLife") as GameObject;
         orangeGhost = GameObject.Find("OrangeGhost");
         redGhost = GameObject.Find("RedGhost");
         pinkGhost = GameObject.Find("PinkGhost");
         blueGhost = GameObject.Find("BlueGhost");
     }

I am currently spawning 4 instances of the same prefab using this script:

     void SpawnLives() {
             for(int i=0; i<4; i++) {
                 firstLife.y += 4.5f;
                 lifeClone = Instantiate(pacmanLives, firstLife, Quaternion.identity);
                 Occurrences.Add(lifeClone);
             }
         }
 

This part is working as when I press play, all 4 prefabs gets instantiated in the position I want them to.

Now my problem is here:

 void DecreaseLives() {
         int lives = Occurrences.Count - 1;
         while (lives >- 1) {
             //HitObject = null;
             Destroy(Occurrences[lives]);
             Occurrences.RemoveAt(lives);
             lives--;
         }
     }

What I need the script to do is whenever any of the ghosts that are tagged as "aiAgent" from Unity's UI collide with the player, I need one life to decrease.

This is the collision code:

 private void OnTriggerEnter2D (Collider2D collision) {
         HitObject = collision.gameObject;
     }
 
     private void OnCollissionEnter2D (Collider2D collision) {
         HitObject = collision.gameObject;
     }
 
 void FixedUpdate () {
 if (HitObject != null && HitObject.tag == "aiAgent" && canKillGhost == false) {
             DecreaseLives();
             Debug.Log("Test Hit");
         }

Note that this script is attached to the player and both the ghosts and the player have a Box Collider 2D. Finally, the player has a Dynamic Rigidbody 2D and the ghosts have a Kinematic Rigidbody 2D.

Thanks!

Comment
Add comment · Show 1
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 Vendrict · Jan 26, 2019 at 10:26 AM 0
Share

I forgot to state that it is not registering the collision - so this might be the problem. Therefore the Debug.Log("Test Hit"); is not being outlined on runtime.

1 Reply

  • Sort: 
avatar image
0

Answer by Vendrict · Jan 26, 2019 at 11:34 AM

Okay, a user by the name of Pulni#5958 from the http://discord.gg/gamedev Discord channel gave me the solution.

This is what I had wrong:

I had to change this:

 private void OnCollissionEnter2D (Collider2D collision) {
          HitObject = collision.gameObject;
      }

to

 private void OnCollissionEnter2D (Collision2D collision) {
         HitObject = collision.gameObject;
     }

Afterwards, I had to tick the Is Trigger from Unity's UI for the ghosts.

alt text

Then, I had to uncomment the HitObject = null.

Finally I reverted the while loop to and if, else if:

 int lives = Occurrences.Count - 1;
 
         if(lives == 0) {
             Debug.Log("No more lives are left. You lose");
         }
         else if (lives > -1) {
             HitObject = null;
             Destroy(Occurrences[lives]);
             Occurrences.RemoveAt(lives);
             new WaitForSeconds(1.5f);
         }



boxcol2.png (10.8 kB)
Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

172 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Adding Rigidbody2d to gameobject removes it from game 0 Answers

Issues with rigidbody, boxcollider, and gravity in a 2d top down game 1 Answer

Using BoxCollider2D as prefab 0 Answers

Unity : How to handle compound colliders 2D physics collision detection 0 Answers

How do i stop my cube sprite from tilting? 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