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 Drexster · May 26, 2011 at 02:33 AM · arraydestroyinstance

Destroy instances from an array

Hi, i'm new to the forums and Unity/JS. I've been banging my head on this for 2 days now. i've tried so many different solutions and have looked at every relevant post online.

The worst part is that i swear there is a very simple solution to this.

I've got a grid of instantiated objects (currently all from a single prefab) which i'm adding to an array by mouse clicking the object. Once i reach the desired selection count I'd like to Destroy those instances and remove them from the array.

Here's the relevant script:

                 for(var i : int =0; i < selected.length; i ++)
                 {
                     //set the current selected object to "thisObject"
                     var thisObject = selected[i]; //this was used by a different failed attempt to Destroy the instances...


                     //used for debuging, change the material to indicate the object is un-selected
                     //thisObject.transform.renderer.material.color = Color.white;    
                     
                     
                     //find the CleanUp script and assign it to the variable "otherScript"
                     var otherScript : CleanUp;
                     otherScript = GetComponent("CleanUp");
 
                     //script name. function name(function parameter)
                 otherScript.CleanThis(true, thisObject);
                 //remove the current object from the array
                 selected.RemoveAt(i);    
                 //add score
                 score++;
 
                 }
             }
     }
     }

The above script is on an empty object in my scene. On my prefab object there is the "CleanUp" script which looks like this:

 //static var alive : boolean = true;
 //var destroyTime : int;
 //var emitParticles : Transform;
 
 function CleanThis(clean : boolean, theObject)
 {
     if(clean == true){
     Destroy(theObject);
     //Instantiate(emitParticles, transform.position, Quaternion.identity);
     //Destroy(theObject, destroyTime);
 }
 }

Any help would be greatly appreciated. If you can spell it out, so i can learn from it/my mistakes, that would be even better!

Thanks.

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 hellcats · May 26, 2011 at 06:33 AM

I think your problem is doing the RemoveAt(i) from inside the loop. RemoveAt will remove the item from the array and shift everything after it down by one. This means that what was at index 4 now is at index 3. Well, if i was 3, then at the end of the loop it increments to 4, the value at 4 is now the value that was at 5 on the last iteration. Effectively you will be removing every other item until you reach the half-way point, and then you will be indexing off the end of the array (which causes an error).

The fix is to just wait until after the loop and do 'selected.Clear()'

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 Drexster · May 26, 2011 at 06:46 AM 0
Share

Wicked that worked!

Thank you!

avatar image
0

Answer by Drexster · May 26, 2011 at 06:32 AM

I've gotten a bit closer after a break, some food and fresh air. I now have 2/3 of my objects destroying, only the first and third objects from the array are destroying. I'm assuming it's something to do with one of my for loops?

here are the iterated scripts:

 if(selected.length == minSelection)
     {
         var isTheSame : boolean = false;
         for(var y : int = 0; y < selected.length; y ++)
         {
             if(selected[0].GetComponent("ObjectClass").objectType != selected[y].GetComponent("ObjectClass").objectType)
                 {
                 isTheSame = false;
                 Debug.Log("You do not have the same selected");
                 }
             
             else
             {
                 isTheSame = true;
                 Debug.Log("You have the same selected!");                
                 for(var i : int =0; i < selected.length; i ++)
                 {
                     var thisObject : GameObject = selected[i].gameObject;
                     var otherScript = thisObject.GetComponent(CleanUp);
                     otherScript.CleanThis(true, thisObject);
                     selected.RemoveAt(i);
                     score++;
                     if (i == selected.length)
                     {break;}
                 }
             }
         }
     }

and the CleanUp script:

 var destroyTime : int;
 var emitParticles : Transform;
 
 function CleanThis(clean : boolean, theObject : GameObject)
 {
     if(clean == true)
     {
         Destroy(theObject.gameObject, destroyTime);
         Instantiate(emitParticles, theObject.transform.position, Quaternion.identity);
     }
 }


Getting closer. Any thoughts/comments?

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 GTMJP13 · Dec 29, 2012 at 09:02 PM

function Update () { for(var i : int = 0 ; i < 5 ; i++) {
var position : Vector3 = Vector3(Random.Range(-10.0,10.0),Random.Range(-10.0,10.0),40.0); go[i] = Instantiate(prefab,position,Quaternion.identity); } for(var j : int = 0 ; j < 5 ; j++) { Destroy(go[j]);
} whats wrong with this code? Please help me out ..novice.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Duplication(instance) re-create deleted children 0 Answers

Problem destroying cloned prefabs 1 Answer

Adding up instances of a variable across different objects? 1 Answer

What is wrong with this script? 2 Answers

How can I destroy objects and instantiates new in Array? 2 Answers


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