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 Drexster · Jan 30, 2012 at 10:36 PM · arraysselection

Removing objects from array in reverse order

I have a selection mechanic that will select objects underneath a drag gesture. I'm trying to deselect the objects when retracing the gesture backwards... the code bellow works for deselecting objects when going over them again, but i want to limit it to the order the objects are in the array. Essentially so objects can only be deselected in the reverse order they were selected in, including the last. if that makes sense.

 else if(hit.transform.GetComponent("ObjectSelected").isSelected == true && dragging)
     {
         for(var zz : int = selected.length-1; zz >= 0; zz--){
                 
                 zz -= 1;
                 if(selected[zz] == hit.transform)
                 {
                 selected.RemoveAt(zz);
                 hit.transform.GetComponent("ObjectSelected").Selected(false);
                 //Debug.Log("Selected object index length= " + selected.length);
                 
                 //selected.RemoveAt(selected.length-1);
                 //selected[selected.length-1].transform.GetComponent("ObjectSelected").Selected(false);
                 break;
                 }
                 
             }
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 Tasarran · Jan 30, 2012 at 10:42 PM

Editing: I just noticed that you are using RemoveAt, which means the array is a JS array.

Just use Pop():

LastItem = Array.Pop()

This would remove the last entry from the array and assign it to LastItem.

Shift() would remove from the beginning.

Comment
Add comment · Show 4 · 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 · Jan 30, 2012 at 11:53 PM 0
Share

it is a built in array. I'm using the Pop like so:

 else if(hit.transform.GetComponent("ObjectSelected").isSelected == true && dragging) 
     {
     var secondLastItem = selected[selected.length-1].gameObject;
     if(hit.gameObject == secondLastItem){
     selected[selected.length-1].transform.GetComponent("ObjectSelected").Selected(false);
     hit.transform.GetComponent("ObjectSelected").isSelected == false;
     selected.Pop();
     }

But this results in a toggle on the first object. How could access the second last object in the array, i get errors on array[array.length-2]...

avatar image Drexster · Jan 31, 2012 at 12:04 AM 0
Share

i.e. i get " Index is less than 0 or more than or equal to the list count." when using:

 else if(hit.transform.GetComponent("ObjectSelected").isSelected == true && dragging) 
     {
     for(var zz : int = selected.length-1; zz >= 0; zz--){
     zz -= 1;
     var secondLastItem = selected[zz].transform;
     
     if(hit.transform == secondLastItem){
     selected[selected.length-1].transform.GetComponent("ObjectSelected").Selected(false);
     hit.transform.GetComponent("ObjectSelected").isSelected == false;
     selected.Pop();
     }

Which works, but i'd like to not have any errors...

avatar image Tasarran · Jan 31, 2012 at 01:44 PM 0
Share

Bracket any operations on the array with an

if (Array[x]) { }

In other words, test to make sure there is a value where you are about to manipulate, before you do it.

Or if you are going to manipulate selected[selected.length-2], then test to make sure that selected.length >= 2.

avatar image Drexster · Jan 31, 2012 at 05:43 PM 0
Share

Thanks for the help Tassaran. I ended up testing against >2, which worked.

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

Grouping objects in level editor 0 Answers

Trying to make gameObjects the target on right click 2 Answers

Can no longer get Array length? 1 Answer

using char to extract integer numbers from a list - [UnityScript] 1 Answer

2d array: finding out if an item is null?!? 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