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 thendricks · Nov 01, 2013 at 08:39 PM · aiarrayslists

How to add to list if not in list

I've been creating a sighting system which will add players to an array of seen targets if they come within the trigger and then i'll be doing some raycasting to check weather they're in front of me. The problem i'm having is adding the object to the list if it isn't in the list. The way it hopefully will work is this line will be called when an enemy has wandered into the trigger(by the enemy)

 function OnTriggerEnter(other : Collider)
 {
     target.gameObject.SendMessage("AddToArray", gameObject,SendMessageOptions.DontRequireReceiver);
 }

and this is called by the ally when the enemy sends the message

 import System.Collections.Generic;
 
 var enemies : List.<GameObject>;
 
 function AddToArray (newTarget : GameObject)
 { 
         enemies.Add(newTarget);
 }

 

As I said there was no error, just I want it to add the enemy to the list, if it isn't in the list

thanks in advanced

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

3 Replies

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

Answer by Dave-Carlile · Nov 01, 2013 at 08:43 PM

You can use the collection's IndexOf method. It will return -1 if the object isn't found.

 if (enemies.IndexOf(newTarget) < 0)
 {
     enemies.Add(newTarget);
 }


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 Dave-Carlile · Nov 01, 2013 at 08:55 PM 1
Share

@Rutter's List.Contains is the better function for this one, so go with that.

avatar image
3

Answer by rutter · Nov 01, 2013 at 08:45 PM

You can use List.Contains():

 if (!enemies.Contains(newTarget)) {
     enemies.Add(newTarget);
 }

Bear in mind that can be an expensive call if your list is huge (hundreds or thousands of items).

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

Answer by Huacanacha · Nov 01, 2013 at 08:58 PM

If uniqueness of items is important and order doesn't matter consider using a HashSet. This will ensure items in your collection are not duplicated, and is much faster if you need to insert into a large collection frequently (it's O(1) to determine uniqueness instead of O(n)).

I'm not sure how to do it in UnityScript but in C#:

 HashSet<GameObject> hs = new HashSet<GameObject>();
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

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

Related Questions

Instantiated Enemy to Recognise Array of Waypoints 2 Answers

Inventory String error 1 Answer

Get a List of the declared objects in a class 1 Answer

How do I moveTowards all the items in a list one at a time? 1 Answer

Argument if out of range: loops and arrays 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