# Enemy Ship Detection

**URL:** <https://discussions.unity.com/t/enemy-ship-detection/419440>\
**Category:** Scripting\
**Tags:** Scripting\
**Created:** [July 30, 2010, 4:50pm UTC](https://discussions.unity.com/t/enemy-ship-detection/419440 "2010-07-30T16:50:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon\_10675285](https://avatars.discourse-cdn.com/v4/letter/a/cc9497/32.png) [@anon\_10675285](https://discussions.unity.com/u/anon_10675285)\
**Post date:** [July 30, 2010, 4:50pm UTC](https://discussions.unity.com/t/enemy-ship-detection/419440/1 "2010-07-30T16:50:40Z")

</div>

Code below is my attempt at detecting an hostile ship at radar range but it is not working correctly at the moment. All the help I can get is welcome:smile:

```auto
var target : Transform;
var radarRange = 500.0;

function Update () {
	if(!target){
		if( (transform.Find( "UNSFDaedalus" ).position - transform.position).magnitude < radarRange ){
			target = transform.Find( "UNSFDaedalus" );
		}
   }else{
   DoSomthing();
   }
}

```

Also in the long run I am thinking of reading list of the enemy ships from another script(an array) any ideas welcome:smile:

---

<div class="post-metadata">

**Author:** ![anon\_51720730](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@anon\_51720730](https://discussions.unity.com/u/anon_51720730)\
**Post date:** [July 30, 2010, 4:53pm UTC](https://discussions.unity.com/t/enemy-ship-detection/419440/2 "2010-07-30T16:53:22Z")

</div>

Try sqrMagnitude instead of magnitude.

Also, as a recommendation, don’t do a Find on each Update. Save that to a variable in the Start method and access it directly.

---

<div class="post-metadata">

**Author:** ![anon\_51720730](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@anon\_51720730](https://discussions.unity.com/u/anon_51720730)\
**Post date:** [July 30, 2010, 4:54pm UTC](https://discussions.unity.com/t/enemy-ship-detection/419440/3 "2010-07-30T16:54:13Z")

</div>

Also, make sure the distance of 500.0 is ok for your scale.

You can make a Debug.Log to print the distance (sqrMagnitude) just to be sure of that.

---

<div class="post-metadata">

**Author:** ![anon\_10675285](https://avatars.discourse-cdn.com/v4/letter/a/cc9497/32.png) [@anon\_10675285](https://discussions.unity.com/u/anon_10675285)\
**Post date:** [July 30, 2010, 5:38pm UTC](https://discussions.unity.com/t/enemy-ship-detection/419440/4 "2010-07-30T17:38:08Z")

</div>

sqrMagnitude is a good one thanks. However I need a method that can lock on to the spawning ships(which does not exist at Start()).

---

<div class="post-metadata">

**Author:** ![ackyth](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/ackyth/32/365287_2.png) [@ackyth](https://discussions.unity.com/u/ackyth)\
**Post date:** [July 30, 2010, 6:23pm UTC](https://discussions.unity.com/t/enemy-ship-detection/419440/5 "2010-07-30T18:23:47Z")

</div>

Try makeing one script a Master for the game have it contain all info you want and then when you need to get the list of units/ships in the game acsess it.

What I do is have the list of all units in game, when a unit is spawned it tells the list to add this unit. When a unit dies it removes itself from the master list.

Then when I want to see what units are nere this unit I do a “for loop” though the master list and get the distance for them and if they are nere this unit I add them to a target list that each unit carrys.

When spawning a unit I just put this in Start() on the unit.

```auto
	void Start () 
    {
        GameObject tempGameMasterGO = GameObject.FindGameObjectWithTag("GameMaster");
        gamemaster = (GameMaster)tempGameMasterGO.GetComponent(typeof(GameMaster));
        unitSelf = this;
        gamemaster.masterUnitList.Add(unitSelf);
	}

```

I store my units not by gameobject but by the script that has all the unit info. If i need to get the gameobject its as easy as: Unit.gameObject

---

<div class="post-metadata">

**Author:** ![ForceX](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/forcex/32/223221_2.png) [@ForceX](https://discussions.unity.com/u/ForceX)\
**Post date:** [July 30, 2010, 7:59pm UTC](https://discussions.unity.com/t/enemy-ship-detection/419440/6 "2010-07-30T19:59:35Z")

</div>

You could try something like this. I’m at work so i can’t confirm that the code works or if it’s even typed correctly, but the jift of it’s in there somewhere.

```auto
var EnemyTag : String = "Enemy";
var EnemyList : GameObject[];
var Target : GameObject;
var distane : float;
var radardistance : float;

function Update(){

Player = transform.position;

EnemyList = GameObject.FindGameObjectWithTag(EnemyTag);

for(var Enemy : GameObject in EnemyList){
	distance = (Enemy.transform.position - Player).magnitude;
	}
	if(distance < radardistance){
		DoSomething(Enemy);
	}
}
function DoSomething(Enemy){

}
}

```
