Search Unity

Is it possible to find an object by variable?

Discussion in 'Getting Started' started by DimaDimov, Dec 10, 2019.

  1. DimaDimov

    DimaDimov

    Joined:
    Dec 1, 2019
    Posts:
    2
    Hi everybody!
    I have a ship in my game it has port and right board situated guns. Guns search for the targets in array with tag
    GameObject[] targets = GameObject.FindGameObjectsWithTag(enemyTag); Tag = Enemy
    But I recenty found, that in a condition whenmy ship is attacked not from 1 side bu t from both sides guns still looking for the only nearest and half of guns "shoot" through the ship.

    I haven't managed to come up with anything but for splitting the tag by parts like Enemy+PortBoard etc. Taking in consideration that there are 300 ships in the game with standard behaviour - it is not possible to make 180 000+ tags. So it must be a variable of type String or a number composed automatically. But I have not found a way to form the enemy array by a variable.

    Of course it is wrong but I need something like this:
    GameObject[] targets = GameObject.FindGameObjectsWithString(Friend1);
    GameObject[] targets = GameObject.FindGameObjectsWithVar(1000001);

    Please advise.
     
    Last edited: Dec 10, 2019
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    So this variable is on an attached MonoBehaviour script, right? The best way is you maintain a list of this script attached to all your ships. Whenever you add a ship you add it to the list, whenever you remove a ship you remove it from the list. Then you just iterate the list. Much faster than calling any of the Find methods all the time, or looping through the list calling GetComponent on each one.

    So attached to the ships something like this
    Code (csharp):
    1. public class ScriptWithVariable : MonoBehaviour
    2. {
    3.     public string MyString;
    4. }
    Elsewhere you maintain a central list of all the attached ScriptWithVariable components
    Code (csharp):
    1. public List<ScriptWithVariable> Ships;
    And when you want to find a specific ship
    Code (csharp):
    1. //Find a specific ship based on a string
    2. //Returns null if the ship doesn't exist
    3. public GameObject ReturnCorrectShip(string correctShipString)
    4. {
    5.     GameObject returnValue = null
    6.     foreach (ScriptWithVariable swv in Ships)
    7.     {
    8.         if (swv.MyString == correctShipString)
    9.         {
    10.             returnValue = swv.gameObject;
    11.         }
    12.     }
    13.     return returnValue;
    14. }
    Then you just got to make sure you maintain the list, which is pretty easy. After you instantiate a ship you add it to the list, and in an OnDestroy method (or whatever method you call when you remove a ship from the game) you remove it from the list.
     
    DimaDimov likes this.
  3. BenVenNL

    BenVenNL

    Joined:
    Oct 14, 2019
    Posts:
    56
    Can't you set a shooting range for each 'cannon'. A large collider as trigger.

    Use the OnCollisonEnter to trigger a method wich adds the particular GameObject with tag "enemy" to a List.
    Use the OnCollsionExit to remove that particular GameObject from that List when it gets out of range.

    Each cannon should automaticcally shoot at the closest partrtcular enemy ship in the List. Wich one, could be determined by the .magnitude of the Vector beween cannon and enemyship position.
     
  4. DimaDimov

    DimaDimov

    Joined:
    Dec 1, 2019
    Posts:
    2
    Thanks everybody who answered. I tried to solve the probleme
    by tags. But nothing happens. Guns still aim the opposite board aims. Here is the script. The best way to cope with probleme is to switch the script off. BUT EVEN THIS DOES NOT WORK. The guns aim with the script switched off.

    1. protected IEnumerator FindClosestTarget()
    2. {
    3. while (true)
    4. {
    5. Transform closest = null;
    6. GameObject[] targets = GameObject.FindGameObjectsWithTag(«Enemy2»);
    7. float distance = sqrVisionRadius;
    8. foreach (GameObject go in targets)
    9. {
    10. Vector3 diff = go.transform.position - transform.position;
    11. float curDistance = diff.sqrMagnitude;
    12. if (curDistance < distance)
    13. {
    14. closest = go.transform;
    15. distance = curDistance;
    16. }
    17. }
    18. target = closest;
    19. nearest = target.name;
    20. yield return new WaitForSeconds(searchTimeDelay);
    21. }
    22. }