Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Rachini · Oct 25, 2019 at 03:39 AM · destroy-clones

Destroying clones of a prefab from another script

i have a gun shooting bullets and i do not want those bullets to run indefinitely so i want to destroy them. i have 2 scripts:

Spawner (spawns the bullets from a bullet prefab: the gun)

bullet = (GameObject) Instantiate(bulletPrefab, transform.position , Quaternion.Identity);

Destroyer( destroys bullets if they go too far away )

public Spawner bulletSpawner;

if (bulletSpawner.bullet.transform.position.z<-1000) { Destroy(bulletSpawner.bullet); }

how can i reference the bullets from the destroyer so it actually destroys the clones? (if i fire 1 bullet it works fine, but i keep firing, it doesnt work)

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Slink472 · Oct 25, 2019 at 03:57 AM

You can add a reference from the spawner script to send the information about the bullets it creates to the destroyer. Then, the destroyer can make a list of these bullets and continuously check how far the bullets have moved before it destroys them. For instance:

Spawner script:

 public GameObject bulletPrefab;
 public Destroyer destroyer;
 
 void Update()
 {
 // Insert whatever conditions you need to meet to fire the bullet
 SpawnBullet();
 }
 
 void SpawnBullet()
 {
 bullet = GameObject Instantiate(bulletPrefab, transform.position, Quaternion.Identity);
 destroyer.SendBullet(bullet);
 }


And your destroyer script might have:

 public list<GameObject> bullets;
 
 void Update()
 {
 foreach(GameObject bullet in bullets)
 {
 if(bullet.transform.position.z < -1000)
 {
 Destroy(bullet);
 bullets.Remove(bullet);
 }
 }
 }
 
 
 public void SendBullet(GameObject bullet)
 {
 bullets.Add(bullet);
 }
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
avatar image
0

Answer by Rachini · Oct 25, 2019 at 09:04 AM

@Slink472 the solution you mentioned is working. but i am getting this error:

InvalidOperationException: Collection was modified; enumeration operation may not execute. System.ThrowHelper.ThrowInvalidOperationException (System.ExceptionResource resource) (at :0) System.Collections.Generic.List`1+Enumerator[T].MoveNextRare () (at :0) System.Collections.Generic.List`1+Enumerator[T].MoveNext () (at :0) Destroyer.Update () (at Assets/Scenes/Destroyer.cs:26)

what does this mean ? and how to fix it ?

Comment
Add comment · Show 2 · 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 GrayLightGames · Oct 26, 2019 at 04:12 PM 0
Share

I believe this occurs when you remove elements from a list that you are iterating on with a loop. One option you could try is to copy the list, iterate on the copy but apply the destruction/removal to the original. You should also iterate from end to beginning so that as you remove elements, it does not mix up the index of the next processed elements. Hope that helps!

avatar image Rachini GrayLightGames · Nov 01, 2019 at 05:06 PM 0
Share

hi @graylightgames , im pretty overwhelmed by this since this is a new concept for me. can you explain more on how to script that please ?

avatar image
0

Answer by GrayLightGames · Nov 02, 2019 at 04:11 AM

No problem @Rachini, things can be overwhelming but hang in there and they will make sense! Just to elaborate a little bit... when you remove items from a list mid-loop, it alters the list and C# has a hard time deciding how to continue looping... So to avoid this, you can copy the list and loop on the copy. Then you can use the items from the copy to reference the original list and modify it. So since you aren't modifying the list that is used for the loop, C# stays happy. When I mentioned end to beginning, I didn't pay attention that you're using a foreach... so you can disregard for this case. If you use a standard for loop, you will refer to the list items by their index, so if you remove the first object, all the other objects have their indexes updated so the list copy and the original will have different indexes and be out of sync. If you start at the end of the list and move backward, this isn't an issue because removing a list item only effects items that are after it. If that is confusing wording, please don't pay attention. If you Google "modifying a collection from a loop", you will see some more detailed discussions of what issues can be caused and what solutions are best. I find the loop copy simple and safe, so that's what I normally do. Hopefully this will resolve the issue for you though and you can get back to the fun stuff!

Here's a code example for your case:

 void Update()
         {
             //Make a temporary copy of the original List object
             bulletsCopy = new List<GameObject>(bullets);
 
             //Iterate on the copy because it will be unchanged during our loop
             foreach (GameObject bullet in bulletsCopy)
             {
                 if (bullet.transform.position.z < -1000)
                 {
                     Destroy(bullet);
                     //Remove from the original list, which works because 
                     //this bullet object exists in both the copy and the original
                     bullets.Remove(bullet);
                 }
             }
         }
Comment
Add comment · Show 2 · 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 Rachini · Nov 08, 2019 at 07:53 AM 0
Share

@graylightgames a Very articulate and elaborate answer ! you explained the stuff very nicely and the thought process behind it also ! really awesome of you.thanks a lot !

avatar image GrayLightGames Rachini · Nov 08, 2019 at 11:08 PM 0
Share

No problem at all, it was a confusing issue the first time I encountered it as well... good luck with your project!

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

115 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

Related Questions

How to destroy bullets? 2 Answers

Destroying clones? 1 Answer

Destroying Children Individually (Horrible..) 2 Answers

How to determine which GameObject to destroy? 2 Answers

Script won't destroy prefab clones when overlapping... plz help 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