Search Unity

Generator Finds Electric devices around itself-Works.| But disconnects them one at a time in a loop.

Discussion in 'Scripting' started by Reedex, Jun 20, 2019.

  1. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    This is how i find devices in range and turn them on
    Code (CSharp):
    1. public void SearchTick()
    2.     {
    3.         nextSearchTick = Calendars.gameTimer + 55;
    4.         List<Collider2D> foundThingies = new List<Collider2D> (Physics2D.OverlapCircleAll (transform.position, radius));
    5.         if (foundThingies.Count > 0)
    6.         {
    7.             for (int i = 0; i < foundThingies.Count; i++)
    8.             {
    9.                 if (foundThingies[i].GetComponent<ElectricDevice>() != null)
    10.                 {
    11.                     if (!connectedDevices.Exists (x => x.name == foundThingies [i].name))
    12.                     {
    13.                         ConnectDevice(foundThingies [i].gameObject.GetComponent<ElectricDevice> ());
    14.                     }
    15.                 }
    16.             }
    17.         }
    18.     }
    I have no idea how to disconnect them properly if the generator gets past the radius.
    Tried a few things, but it seems like no matter from which angle i step in, it's always wrong.
    It should go like this:
    If our OverlapCircleAll haven't find that, what is already in the connectedDevices list..
    Then remove from the list that what was not found.
    sounds easy but i just can't find out how to do it.
     
  2. Team2Studio

    Team2Studio

    Joined:
    Sep 23, 2015
    Posts:
    98
    you could turn the logic around and let the devices decide for themselves wether to turn on or off based on a distance check, and make them tell that to the generator so it can remove or add the device from/to the list.
     
    Reedex likes this.
  3. Team2Studio

    Team2Studio

    Joined:
    Sep 23, 2015
    Posts:
    98
    also, the return value for physics2D.OverlapCircleAll is an array, which you could test against your list before adding or removing to/from the list.
     
    Reedex likes this.
  4. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    Yes Thank you. :- )
     
  5. Team2Studio

    Team2Studio

    Joined:
    Sep 23, 2015
    Posts:
    98
    glad I could help :)
     
    Reedex likes this.