Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] Can I use a Lamdba here?

Discussion in 'Scripting' started by Deleted User, Dec 19, 2014.

  1. Deleted User

    Deleted User

    Guest

    I have this code block in my script with an if/then/else but I want to know if it can also be done with a Lambda instead and if so, how I can implement it.

    Code (CSharp):
    1. void DoThis(){
    2. var enemies = FindComponentsOfType(typeof(Stats))..gameObject.where x != gameObject;
    3. }
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Not sure what FindComponentsofType is doing but if you just mean the where clause then

    Code (csharp):
    1.  
    2. FindComponentsOfType(typeof(Stats)).Where(x => x.gameObject != gameObject);
    3.  
     
  3. Deleted User

    Deleted User

    Guest

    FindComponents returns an array of components of the type specified. I only want it to return those that are not the game object itself.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I more meant - "This should work assuming your method is returning an IEnumerable"
     
  5. Deleted User

    Deleted User

    Guest

    Object does not implement IEnumerable so it's not working. :(
     
  6. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I don't think that's the right way to handle that. If you're going to be operating on IEnumerable<Component> anyway, use one to start with. In general, though, you posted too much that was irrelevant to the question, or unknown to us. Please try to provide more clarity in the future.
    Code (CSharp):
    1. void DoThis() {    // Why is this relevant to your question?
    2.     var enemies // This name doesn't make sense. You're getting Stats. Stats are not enemies.
    3.         = Components // What goes here? Is it even relevant to your question?
    4.         .OfType<Stats>()
    5.         .Where(stats => stats.gameObject != gameObject);
    6. }