Search Unity

Question How do I check If anything is in a list?

Discussion in 'Scripting' started by themushroomdragon, Dec 16, 2022.

  1. themushroomdragon

    themushroomdragon

    Joined:
    Jun 19, 2020
    Posts:
    2
    I'm trying to check if anything is in a list with the code shown, but it always activates even when there is nothing in the list.
    Code (CSharp):
    1. if (fov.targetsinvison != null)
    2.       {
    3.          
    4.             engaged = true;
    5.          
    6.        }
    How would I check to see if there is anything in the list?
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    Just check the size of the List.
     
    Reedex likes this.
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,443
    When in doubt, there's *documentation*.

    https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1

    For any class in System, Microsoft will have documentation on it. For any other class Unity provides, Unity will have documentation on it.

    As a new developer, it's also a good idea to just browse documentation like this for class names you're becoming familiar with, just aimlessly and out of pure curiosity. It will speed your awareness and understanding of how everything fits together. It will give you new ideas on how to solve older problems better, or solve problems you haven't had yet.

    When documentation was in book form, I would keep a couple API manuals in my bathroom for light reading. Nowadays there's smartphones and any API reference is at your fingertips.