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 Chimera3D · Aug 26, 2012 at 01:59 AM · functionarraysresizingbuilt-in

What's wrong with this code?

Okay, this is my attempt at adding a remove function for the built-in unity arrays. I've played around with the variables and for loop limits for hours and nothing seemed to work. What it does is it takes an array, then puts it's contents into another array, then it takes each element in the second array after the element that has to be removed it will just replace it with the next element, then it will go and put those elements back into the modArray but without the last element, so theres one less element which basically removes that element. But for some reason this isn't working, as it removes all but the first element in the array, it's based off of my add function which works flawlessly and faster than the standard JS arrays.

 function RemoveObject () { 
 
     var refArray : GameObject[];
     var index : int;
     var index2 : int;
     
     refArray = modArray;
     
     var length = (refArray.length -2);
     
     modArray = new GameObject[(length)];
     
     var modLength = modArray.length;
     var refLength = refArray.length;
     
     for(var i = 0; i < modLength; i++){
         
         index2 = i;
         
         if(refArray[i].transform.position == modElement.transform.position){
             
             index = i;
             
         }
     }
     
     if(index2 < modLength && index > 0){
         
         refArray[index2] = refArray[index2 +1];
             
     }
         
     for(var x = 0; x < modLength; x++){
     
         modArray[x] = refArray[x];
         
     }
 }
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 Eric5h5 · Aug 26, 2012 at 02:27 AM 0
Share

Why don't you just use List, ins$$anonymous$$d of trying to re-invent the wheel? There's no reason to do this.

1 Reply

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

Answer by Bunny83 · Aug 26, 2012 at 02:43 AM

There are several things strange:

  • Your new array is 2 elements shorter than the original one. when you want to remove one element you should decrease the lenth by 1, not by 2

  • Your index2 variable will always end up with the highest value possible in your new array. That's pointless since you could always use modLength-1

  • It seems you're searching for a certain position. If you want to search for a certain object it's better to compare the objects itself and not their positions. If you just want to test for the position it's fine that way, but keep in mind that the position have to match "exactly". The position (1.0, 50.5, 45.87) and (1.0, 50.5, 45.8700001) appear at the same spot, but they're not equal.

  • When you search for an index and you've found it, you should break the loop since it's pointless to iterate through the whole array.

  • You copy the second last element of your original array at the last position of your new array, but then you run the for loop at the end which just overwrites all elements and just copies all elements from the original array into the new one but removes the last two objects...

  • Last thing is your function seems to work with certain class variables, so it's not a general approach.

A function to remove a certain gameobject from a gameobject array could look like this:

 static function RemoveGO(arr : GameObject[], obj : GameObject)
 {
     var index = -1;
     for (var i = 0; i < arr.length; i++)
     {
         if (arr[i] == obj)
         {
             index = i;
             break;
         }
     }
     if (index == -1) // the element wasn't found
         return arr;
     var tmpArr = new GameObject[arr.length - 1];
     for(i = 0; i < index;i++)
         tmpArr[i] = arr[i];
     for(i = index+1; i < arr.length;i++)
         tmpArr[i-1] = arr[i];
     return tmpArr;
 }

To use this function you would do this:

 modArray = RemoveGO(modArray, modElement);

Anyway, it would be much easier to use a List. A List uses an native array internally and provides all those functions already. To use a generic list you have to import the System.Collections.Generic namespace:

 import System.Collections.Generic;
 
 var modArray : List.<GameObject>;
 
 
 modArray = new List.<GameObject>();
 
 // Add an element at the end:
 modArray.Add(newGO);
 
 // To remove an element
 modArray.Remove(modElement);
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 Chimera3D · Aug 26, 2012 at 05:32 AM 0
Share

Thanks a lot!

avatar image Chimera3D · Aug 26, 2012 at 07:16 AM 0
Share

Is there anyway to only add the element once, as in when I convert the modArray back to the other array so that the element will only be added once, in only 2 seconds there will will be 4000+ elements.

avatar image Chimera3D · Aug 26, 2012 at 07:20 AM 0
Share

Wait is list as fast as the built-in array system?

avatar image Bunny83 · Aug 26, 2012 at 11:23 AM 0
Share

Almost, the access is almost the same speed. A List just holds an internal native array to store the elements. It usually creates the internal array larger than the element count that is stored. That prevents the list from having to recreate the native array all the time. When you know you have to store about 10000 objects in the list, specify an initial capacity:

 modArray = new List.<GameObject>(10000);

This will create the internal array with a size of 10000 so adding new elements won't force a recreation. Deleting an element just copies the later elements towards 0 by one.

You could do this yourself, but you have to remember how many elements you have in your array and keep the last elements free. But if you do so, just use a List since it's made for exactly this purpose.

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

9 People are following this question.

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

Related Questions

Calling A Function Once 0 Answers

Loading function into array using GameObject.Find 1 Answer

Arrays and indices issues 1 Answer

Passing an array to a function by reference? 1 Answer

Algorithm for loading sounds 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