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. Dismiss Notice

Count GameObjects with Tag in specific Area

Discussion in 'Scripting' started by Tobiberger, Nov 30, 2020.

  1. Tobiberger

    Tobiberger

    Joined:
    Jun 26, 2020
    Posts:
    5
    Hello,

    i hope that this thread does not yet exist and if so i am sorry but i didnt find anything.

    I would like to Count GameObjects via Tag in a specific area, for example all Villagers in one village so that i can respawn them if the count is to low. The villagers that are not in the given area shouldn be counted.

    Please tell me if theres another possibility that does not work over tags.

    Thank you very much
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    If you go about "Villages" and so on, you don't want a "Range Sphere" getting all Objects,
    filter them and check if you need to respawn any Villagers,

    a better approach would be to make a Script which from "Start On" have those Villagers assigned
    or a Spawner with spawn points for Villagers which only cares about those villagers, so you start
    with no villagers and you instantiate them at start of the game and keep track of them
     
    Tobiberger likes this.
  3. Tobiberger

    Tobiberger

    Joined:
    Jun 26, 2020
    Posts:
    5
    thank you for your answer, i did it like you said and it works perfectly
     
    Terraya likes this.