Search Unity

Feedback making a whitelist/ black list

Discussion in 'Scripting' started by HannibleJester, Apr 30, 2021.

  1. HannibleJester

    HannibleJester

    Joined:
    Jan 15, 2021
    Posts:
    9
    so im tryna make a whitelist / blacklist for my selector. I want my selector to check and see if the object that its raycasting too is on the whitelist / blacklist and if it is then ignore it. Here is bits of what I have so far.
    Code (CSharp):
    1. public GameObject highlighted;
    2.  
    3. public List<GameObject[]> whiteList = new List<GameObject[]>();
    4.  
    5.    public GameObject[] bckGround, ground, backGroundWalls, trees;
    6.  
    7.  
    8. void Start()
    9. {
    10. bckGround = GameObject.FindGameObjectsWithTag("BackGround");
    11.         ground = GameObject.FindGameObjectsWithTag("Ground");
    12.         backGroundWalls = GameObject.FindGameObjectsWithTag("BackGroundWalls");
    13.         //trees = GameObject.FindGameObjectsWithTag("Trees");
    14. }
    15.  
    16. void Update()
    17. {
    18. whiteList.Add(bckGround);
    19.         whiteList.Add(ground);
    20.         whiteList.Add(backGroundWalls);
    21.         //whiteList.Add(trees);
    22. SelectObject();
    23. }
    24.  
    25. void SelectObject()
    26. {
    27. if(highlighted = whiteList)
    28.         {
    29.             ClearSelection();
    30.         }
    31. }
    32.  
    33.  
    34.  
    35.  
    its currently saying gameobject does not work with gameobject[] and idk how to work around that but i do know that this is the problem
    Code (CSharp):
    1. if(highlighted = whiteList)
    2.         {
    3.             ClearSelection();
    4.         }
    However I would also like suggests if there is a better way to do this.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,740
    Single equal sign is an assignment.

    Double equal sign is comparison.

    HOWEVER... you are comparing apples and oranges. (a GameObject vs an Array of GameObjects)

    You need to iterate each item in whiteList (or blackList) and check it to see if it matches.
     
    HannibleJester likes this.
  3. HannibleJester

    HannibleJester

    Joined:
    Jan 15, 2021
    Posts:
    9
    im not sure how to do what your saying but my mind is saying to do this with the way you stated it

    Code (CSharp):
    1. if(highlighted == bckGround || highlighted == backGroundWalls || highlighted == ground)
    2.         {
    3.             ClearSelection();
    4.         }

    i also changed
    Code (CSharp):
    1. bckGround = GameObject.FindWithTag("BackGround");
    2.         ground = GameObject.FindWithTag("Ground");
    3.         backGroundWalls = GameObject.FindWithTag("BackGroundWalls");
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,740
    You definitely need to get familiar with the concept of iteration.

    For example:

    Code (csharp):
    1. bool isWhitelisted = false;
    2.  
    3. // iterate
    4. foreach( var item in whiteList)
    5. {
    6.   // compare
    7.   if (highlighted == item)
    8.   {
    9.     isWhitelisted = true;
    10.     break;
    11.   }
    12. }
    13.  
    14. Debug.Log( "whitelisted:" + isWhitelisted);
    Anytime you have a collection you generally need to consider each item one at a time.
     
    stain2319 and HannibleJester like this.
  5. HannibleJester

    HannibleJester

    Joined:
    Jan 15, 2021
    Posts:
    9
    so im not able to make an unlimited list i will have to write everything down and ask the code to search that array?