Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Ninku · Jan 16, 2011 at 05:20 PM · arraydestroybce0048removeat

How to remove objects from an array and destroy

Hi! This is my first question but probably not the last. What I was trying to accomplish with this is to remove the gameobject from the array and destroy it when it collides with a specific object.

The commented line (global.arr.RemoveAt[i];) is currently giving an error as follows: Type 'function(int): void' does not support slicing.

Which makes my head explode. I'm very noob. Is my loop wrong somehow? Is it syntax? Wrong array type for what I'm doing? I'm also not sure that comparing the position is the best way to verify I have the right object from the list. Any help is greatly appreciated!

function OnCollisionEnter(theCollided : Collision){ //when an object collides with something named ground if(theCollided.gameObject.name == "ground"){ //set a variable to the number of entries in the array -1 to get last array entry var ii : int = global.arr.length -1; //loop through the array for(i=ii; i>=0; i--){ //if the object on the list has the same position as the collided... if(global.arr[i].transform.position == gameObject.transform.position){ //remove the object from the list // global.arr.RemoveAt[i]; //print the new list for verification Debug.Log(global.arr); //destroy the object Destroy(gameObject); } }

 }

}

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

1 Reply

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

Answer by Peter G · Jan 16, 2011 at 05:29 PM

Without knowing what global.arr is besides an array, the only problem I see is that your are trying to use an indexer to pass an argument to a function. You should do:

global.arr.RemoveAt(i);

I also don't think that you are destroying the right GameObject. gameobject is the object that the script is attached to. It sounds like you want to destroy the other object in which case you want to do:

//inside your for loop
var deadGB = global.arr[i];
global.arr.RemoveAt(i);
Destroy(deadGB);
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 Ninku · Jan 16, 2011 at 05:44 PM 0
Share

Wow! Seems like I wasted so much time. Thank you for your quick and helpful response!

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

How to destroy the first clone of a UI Image using an array every time I press a button? 1 Answer

Destroy Particle System in an Array 0 Answers

How to destroy an instantiated object when I touch it? 0 Answers

Destroying Instantiated Prefab 1 Answer

How to destroy all the items with one function? 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