# Targetting Script Help

**URL:** https://discussions.unity.com/t/targetting-script-help/40783
**Category:** Questions & Answers
**Created:** [April 21, 2012, 2:19pm UTC](https://discussions.unity.com/t/targetting-script-help/40783 "2012-04-21T14:19:36Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![system](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/system/32/97283_2.png) [@system](https://discussions.unity.com/u/system)
#### Post date: [April 21, 2012, 2:19pm UTC](https://discussions.unity.com/t/targetting-script-help/40783/1 "2012-04-21T14:19:36Z")

</div>

Hi Guys 😃 I Have a question. Is it Possible to Change this script so I “Target” a Enemy when i click on it :). I need Just Help with it to. 🙂 So Here is the Script:

public class Targetting : MonoBehaviour {  
public List targets;  
public Transform selectedTarget;

```
private Transform myTransform;
	
void Start () {
	targets = new List<Transform>();
	selectedTarget = null;
	myTransform = transform;
	
	AddAllEnemies();
}

public void AddAllEnemies() {
	GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
	
	foreach(GameObject enemy in go)
		AddTarget(enemy.transform);
}

public void AddTarget(Transform enemy) {
	targets.Add(enemy);
}

private void SortTargetsByDistance() {
	targets.Sort(delegate(Transform t1, Transform t2) {
			return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
			});
}

private void TargetEnemy() {
	if(selectedTarget == null) {
		SortTargetsByDistance();
		selectedTarget = targets[0];
	}
	else {
		int index = targets.IndexOf(selectedTarget);
		
		if(index < targets.Count - 1) {
			index++;
		}
		else {
			index = 0;
		}
		DeselectTarget();
		selectedTarget = targets[index];
	}
	SelectTarget();
}

private void SelectTarget() {
	selectedTarget.renderer.material.color = Color.red;
	
	PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");
	
	pa.target = selectedTarget.gameObject;
}

private void DeselectTarget() {
	selectedTarget.renderer.material.color = Color.white;
	selectedTarget = null;
}

// Update is called once per frame
void Update () {
	if(Input.GetKeyDown(KeyCode.Tab)) {
		TargetEnemy();
	}
}

```

}

Any Ideas to Start or Help :)?

---

<div class="post-metadata">

### Author: ![Kleptomaniac](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@Kleptomaniac](https://discussions.unity.com/u/Kleptomaniac)
#### Post date: [April 21, 2012, 2:43pm UTC](https://discussions.unity.com/t/targetting-script-help/40783/2 "2012-04-21T14:43:56Z")

</div>

Just use a raycast from your cursor coordinates. Like so:

```
void Update () {
	if(Input.GetButtonDown("Fire1")) {
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);   
		RaycastHit hit;    
		if(Physics.Raycast(ray, hit, 100)) {
			if (hit.collider.gameObject.tag == "enemy") {
				selectedTarget = hit.collider.gameObject;
			}
		}
	}
}

```

You may need to fit it into your code a bit better as you see fit.

Hope that helps, Klep
