Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

how to check if an array contains an int.

Discussion in 'Scripting' started by False_Baconator, Jan 26, 2019.

  1. False_Baconator

    False_Baconator

    Joined:
    Oct 2, 2018
    Posts:
    35
    Hi, I'm kinda a beginner and need some help. my goal is to check if an int array contains a specific int. like for example, a 1. unfortunately, i have no idea how to do this. i tried looking it up and found a bunch of talk about predicates and delegates, and i have no idea how any of it works. I'm fairly confused and just need some help.

    thanks in advance!
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,005
  3. soreric

    soreric

    Joined:
    May 8, 2014
    Posts:
    13
    Lurking-Ninja's answer will also work, but the simplest way to do this:

    myArrayVariable.Contains(1);
     
  4. False_Baconator

    False_Baconator

    Joined:
    Oct 2, 2018
    Posts:
    35
    it says "int[] does not contain a definition for Contains..." so that doesn't seem to be working.
     
  5. False_Baconator

    False_Baconator

    Joined:
    Oct 2, 2018
    Posts:
    35
    i checked out the page, and I'm still nowhere close to understanding how to use a predicate, it just doesn't make sense to me.

    here's some of what i have at the moment
    Code (CSharp):
    1. public int[] openingDir;
    2.  
    3. bool FindB(int var)
    4.     {
    5.         if (var - 1 == 0)
    6.             return true;
    7.         else
    8.             return false;
    9.     }
    10.  
    11.  
    12.     void Spawn()
    13.     {
    14.         if (spawned == false)
    15.         {
    16.             Predicate<int> B = FindB;
    17.  
    18.  
    19.             if (Array.Exists(openingDir, FindB(1))
    at the "FindB(1)" in "if (Array.Exists(openingDir, FindB(1))" it says "Argument 2: cannot convert from "bool" to "System.Predicate<int>"

    i've tried having the type of predicate be bool, or blank, but that message doesn't change.
     
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,005
    Never ever use the word "var" as variable name. :D It's too confusing.

    Like the example says on the link I gave you:
    Code (CSharp):
    1. void Spawn()
    2.     {
    3.         if (spawned == false)
    4.         {
    5.             if (Array.Exists(openingDir, el => el == 1)
    If you don't understand this yet, feel free to use the
    Code (CSharp):
    1. openingDir.Contains(1);
    as @soreric advise you and you can return to the predicates and more complex search-problems later.
     
  7. nat42

    nat42

    Joined:
    Jun 10, 2017
    Posts:
    353
    As a beginner I feel doing this manually might be beneficial.

    Imagine you have a bunch of boxes to look in, all in a line? You might check the first box, and if you don't find what you are looking for you move along to the 2nd, etc. Until you either find what you were looking for or run out of boxes to check...

    How would you write a for loop to look at every element of the array?

    In the for loop how would you check if the element matches the number you are looking for?
     
  8. soreric

    soreric

    Joined:
    May 8, 2014
    Posts:
    13
    Add
    Code (CSharp):
    1. using System.Linq;
    To the top of your file.
     
    Bunny83, Witty1, ethancai920 and 2 others like this.