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 derrtyones · Aug 29, 2012 at 02:37 PM · javascriptarrayremoveat

Removing element from array by value, not index

Hi,

I got an array. One of the elements inside this Array is "item_Key". I would like to know how I can remove this item from the array. I am basically hitting a box, which will trigger to remove "item_Key" from the array. The only problem is I don't know how to do this. I know I can remove an index with RemoveAt() but I am looking to remove by the value.

 gameObject.Find("itemHolder").GetComponent("playerInventory").playerInventory.RemoveAt();




@Andreeeee

 var RemoveItem : String;
 RemoveItem = "item_Key";
 
 var arr = gameObject.Find("itemHolder").GetComponent("playerInventory").playerInventory;
 
 function OnTriggerEnter (other : Collider) {
     
 }
 
 var i = 0;
 
 while (i < arr.Length && arr[i] != RemoveItem)
 i++;
Comment
Add comment · Show 2
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 Kryptos · Aug 29, 2012 at 03:05 PM 1
Share
  1. You cannot remove (or add) items inside a foreach loop.

  2. You misuse GameObject.Find(). This function does not look inside the GameObject hierarchy but looks for the first GameObject with the given name amongst all objects in the scene. You might want to use transform.Find() ins$$anonymous$$d.

avatar image derrtyones · Aug 29, 2012 at 03:10 PM 0
Share

Thank you for the tips, very helpful! I am quite new to scripting so I was just trying things. So how exactly can I remove "item_$$anonymous$$ey" from the array?

2 Replies

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

Answer by andeeeee · Aug 29, 2012 at 03:42 PM

If you are using Unity's Array class then you can use the RemoveAt function. This lets you remove an item by its integer index. You can find the index of the item with the key using a loop like this:-

   var i = 0;
 
   while (i < arr.Length &amp;&amp; arr[i] != key)
     i++;

If you are using a standard .NET array, you can use the following function:-

   function RemoveByName(arr: String[], key: String) {
     var i = 0;
 
     while (i < arr.Length && arr[i] != key)
       i++;
 
     if (i < arr.Length) {
       for (var j = i + 1; j < arr.Length; j++) {
         arr[j - 1] = arr[j];
       }
 
       System.Array.Resize.<String>(arr, arr.Length - 1);
     }
   }

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 derrtyones · Aug 29, 2012 at 03:52 PM 0
Share

I am using the Unity Array class. With your mentioned script I still don't know the index for the item "item_$$anonymous$$ey". Please help!

avatar image andeeeee · Aug 29, 2012 at 03:57 PM 0
Share

You'll notice the loop in the first code sample I posted looks for the key in the array. The i variable contains the index of the key item if it is present in the array.

avatar image derrtyones · Aug 29, 2012 at 04:06 PM 0
Share

Sorry I am completely new to this so I have a hard time understand how this works. I am putting my current script in the first post. At the moment I get a few errors.

avatar image andeeeee · Aug 30, 2012 at 08:33 AM 0
Share

Firstly, you need to put the other code inside the OnTriggerEnter function (ie, between the curly braces) for it to be activated by an object entering the trigger. It seems to be mostly O$$anonymous$$ - after the while loop has finished, the i variable will contain the index of the item to remove. You can pass that index to the RemoveAt function to actually take it out of the array.

avatar image
0

Answer by Bunny83 · Aug 29, 2012 at 03:32 PM

Well, you didn't tell us what playerInventory is, but i guess it's a List? The Remove function does exactly what you want ;)

I can't show you a full example since i don't know what you actually store in your inventory. This example just uses an "Item" class. First you have to import the System.Collections.Generic namespace at the top of your script like this:

 // Unityscript
 import System.Collections.Generic;

 // UnityScript
 var playerInventory : List.<Item>;

To create an instance of a list you would do:

 // UnityScript
 playerInventory = new List.<Item>();




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 derrtyones · Aug 29, 2012 at 03:35 PM 0
Share

I'm sorry! I use javascript. playerInventory is an array. When I hit certain boxes, their tags are stored into this array. I only need to now how exactly I can remove a certain element from the array when I only know the name of the element that has to be removed.

avatar image Bunny83 · Aug 29, 2012 at 03:51 PM 0
Share

That makes no difference. You shouldn't use the Array class at all since it's slow and not type-safe. I add an example in my answer.

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

10 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

Related Questions

Javascript How to Activate/Deactivate GameObject Arrays 1 Answer

Turn all objects in the array to make same thing 0 Answers

InvalidCastException error and logic error 1 Answer

Store an array inside of an array 1 Answer

How to use Javascript Array with classes? 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