• 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
Question by esitoinatteso · Apr 20, 2015 at 09:02 PM · listoptimizationlistsaddexists

Check if Exists and if so, Add it to Other List

Hi there! I've just experienced an odd issue and I'd like to be enlightened on this topic.

I'm posting a piece of code in which I'm inside a switch, but that's not so important; what matters is that I'd like to check if in the list called mirror exists one object with a certain value and if so, add it immediately to a second list called Lpositions.

Since I'm already checking if something like that exists it would be awesome to be able to just address that " x" and say: Lpositions.Add(x.position)! but that returns an error... how sad.

Could you help me?

I'm self-teaching me C# so if you could elaborate a bit your answer or pass me a link of not-too-complicated code I'd really appreciate.

Thanks for your time!

 //Check if something other than rand is empty in our mirror list
             case 2:
 
                 //hypotheticalActorsOnPoint is the value I want to check 
                 if(mirror.Exists(x => x.hypotheticalActorsOnPoint == 0)){
                     
                     for(int j = 0; j < mirror.Count; j++){
                         
                         if(mirror[j].hypotheticalActorsOnPoint == 0){
                             
                             Lpositions.Add(mirror[j].position);
                             mirror[j].hypotheticalActorsOnPoint++;
                             i++;                //this code is inside a while loop too
                             
                             if(mirror[j].isFull){
                                 
                                 mirror.Remove(mirror[j]);
                             }
                             
                             break;
                         }
                     }
 
                     break;
                 }
 
                 else{
 
                     goto default;
                 }

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Mapleman · Apr 21, 2015 at 06:22 AM

Instead of using Exists, you could use Find. Something like this:

 case 2:
    var t = mirror.Find(x => x.hypotheticalActorsOnPoint==0);
    if (t!=null)
    {
        Lpositions.Add(t.position);
        ...
    }

Your question about why you can't use 'x' inside your if statement is bit more complex though. What C# lambda syntax means in your case goes roughly like follows:

The statement below is actually instruction to compiler to generate an anonymous method which takes in one parameter of type x and returns boolean.

x => x.hypotheticalActorsOnPoint == 0

if you had a method with signature: bool MyComparer(T elementInList)

where T is the type of your object stored in list, you could write your if statement like this:

if(MyComparer) { ... }

If you check the msdn documentation for example the Find method I proposed you could use, it's signature is like:

public T Find( Predicate match )

And as it happens the Exists method has exactly the same signature:

public bool Exists( Predicate match )

So keep in mind that whenever you are writing a lambda expression, you are actually declaring a method.

Hope this clarifies things a bit for you.

Comment
esitoinatteso
VaderCmp

People who like this

2 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 esitoinatteso · Apr 21, 2015 at 11:16 AM 0
Share

Awesome!!! Yes that's exactly what I wanted to do, thanks a lot both for the code and the valuable tips about lambdas, I'm starting to understand them a bit more!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Help: items not getting added to list. 1 Answer

A node in a childnode? 1 Answer

How to add an item to a list with a class/uJS struct? 1 Answer

can add to a list but not remove 1 Answer

List.Add just once in Update 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