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
1
Question by theweresloth · Oct 17, 2013 at 07:13 PM · collisionarraytriggerlist

Avoiding adding duplicate objects to ArrayList

code works, bug was elsewhere in the code, thanks to those who helped, was useful to debug

So I have a number of objects, each with a collision trigger to detect when another object gets within a certain radius. It then broadcasts a message to add the object (technically a reference or pointer or whatever I suppose), to an ArrayList with the following code:

 //collision trigger structured for modular use (irrelevant to question as far as I know)
 function OnTriggerEnter (other: Collider) {
     transform.root.BroadcastMessage("AddMatchNeighbor", other.gameObject.transform.root.gameObject, SendMessageOptions.DontRequireReceiver);
 }
 
 //script receiving broadcast
 function AddMatchNeighbor (other: GameObject) {
     if ( other in matchNeighbors == false) {
         matchNeighbors.Add(other);
     } else {
     Debug.Log("Match duplicate");
     }
 }

Objects are removed from the array in a similar fashion OnTriggerExit(). When one of the objects is clicked, it then checks to see if there are 2 or more objects in the array (i.e. 3 or more objects are close to each other) and if so, score is awarded the player and the objects are destroyed (think Match 3 style game mechanic).

The problem is that the ArrayList (matchNeighbors) isn't being reliable in it's accounting and it will get longer overtime with duplicate objects (which my code seems to prevent most of the time, but apparently not all). Any idea what the problem might be?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by LukaKotar · Oct 17, 2013 at 07:52 PM

You could compare the object's ID. If the ID is the same, then it is the same object. You can get the object's ID using gameObject.GetInstanceID.

I wrote some code for you, heavily commented (although my js is not that great):

 function AddMatchNeighbor (other: GameObject) {
     //In the start of the function, create a boolean to determine if the object can be added, and set it to false.
     var canAdd : boolean = false;
     //Create a `for` loop to check if the object you're about to add has the same ID as any of the existing ones in the array.
     for ( var i : int; i < matchNeighbors.length; i++ ) {
         if ( matchNeighbors[i].GetInstanceID() != other.GetInstanceID() ) {
             //If the object's IDs do not match, set the variable to true for the time being.
             canAdd = true;
         } else {
             //Object with the same ID as the 'other' is found.
             //Set the variable to false, and stop the loop.
             canAdd=false;
             break;
         }
     }
     //If the loop was stopped or finished, check the final value of the variable, and add the object if the condition is true.
     if( canAdd == true ){
         matchNeighbors.Add(other);
     } else {
         Debug.Log("The object is already in the array, and won't be added again!");
     }
 }


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 theweresloth · Oct 18, 2013 at 04:09 AM 0
Share

$$anonymous$$y code actually works as it turns out, the problem was elsewhere in my code (D'oh!). Your code helped me debug though, so I certainly appreciate it.

avatar image
0

Answer by Cherno · Oct 17, 2013 at 08:41 PM

I just use

 if(ListToCompare.Contains(ObjectToCompare) == false)




But I suspect LukaKotar's solution is safer, and I am not sure if mine works for ArrayLists, it does work for Generic Lists though :)

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

17 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to organize tracking of collisions? 0 Answers

Collecting Array items in order 1 Answer

Remove object from List after gameObject was destroyed in between Scene loads 1 Answer

Script effects all gameobjects. 1 Answer

A node in a childnode? 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