Search Unity

making a whitelist/ black list

Discussion in 'Editor & General Support' started by HannibleJester, Apr 12, 2021.

  1. HannibleJester

    HannibleJester

    Joined:
    Jan 15, 2021
    Posts:
    9
    I am having problems making a sorta of white list / black list. I want my highlighter to check and see if the item its about to highlight is on the white list / black list first and if it is then do not highlight it. Here is what I got so far.

    Code (CSharp):
    1. public GameObject highlighted;
    2. public List<GameObject[]> whiteList = new List<GameObject[]>();
    3.  
    4.   public GameObject[] bckGround, ground, backGroundWalls, trees;
    5.  
    6. void Start()
    7. {
    8. bckGround = GameObject.FindGameObjectsWithTag("BackGround");
    9.         ground = GameObject.FindGameObjectsWithTag("Ground");
    10.         backGroundWalls = GameObject.FindGameObjectsWithTag("BackGroundWalls");
    11.         //trees = GameObject.FindGameObjectsWithTag("Trees");
    12. }
    13. if(highlighted = whiteList)
    14.         {
    15.             ClearSelection();
    16.         }
    17.  
     
    Last edited: Apr 30, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    You have a LOT going on for what you're trying to do. I suggest breaking things into separate small scripts.

    1. a script to do your camera fly-around

    2. a script to do your ant walk-around on its own

    3. a script to give the player control of an ant (disabled by default)

    4. a script to raycast from the camera, hit a particular ant (via collider, not tag or find!), and when it hits it:
    --> disable script 2 above
    --> enable script 3 above

    And obviously when you stop controlling an ant, you turn off its #3 script and enable its #2 script.

    You could combine #2 and #3 if you must, then use some kind of "being possessed" boolean.

    ALSO, things like line 25 in your camera script is just going to find a random ant, possibly the same one over and over, possibly not.

    ALSO, since the ant script (presently) is always running for all ants, that's why you're seeing this.

    Either way, break it into discrete separate layers so you can isolate when things misbehave.

    Also, just in general to find what's going on, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.

    If you are running a mobile device you can also see the console output. Google for how.
     
    HannibleJester likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Breaking the scripts apart is a suggestion overall just to get a handle on the sheets of code above in order to break it down into smaller parts you can individually test and get working. This way once each part works, you can forget about it, rather than two big blobs of code that do lots of different things all together mixed in.

    The basic idea is this:

    - you need to track whatever you call "camera is attached" or not (some kind of state saying who, if anybody, you're attached to)

    - when attached, disable the contrrol by the ant's normal motion

    - when attached, enable the control of the ant by the player
     
    HannibleJester likes this.