- Home /
Selecting enemies with mouse button
I am trying to program my game so that i can select enemies by just clicking on them here is my script
using UnityEngine; using System.Collections;
using System.Collections.Generic;
public class Targeting : MonoBehaviour { public List targets; public Transform selectedTarget;
private Transform myTransform;
// Use this for initialization
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.blue;
selectedTarget = null;
}
// Update is called once per frame
void Update () {
//Only search if clicked
if( Input.GetMouseButtonDown(0) ){
//Shoot ray from mouse position
Ray ray = Camera.mainCamera.ScreenPointToRay( Input.mousePosition );
RaycastHit[] hits = Physics.RaycastAll( ray );
bool gotTarget = false; //This is so we can deselect if we didn't click anything
foreach( RaycastHit hit in hits ){ //Loop through all the hits
if( hit.transform.gameObject.layer == 8 ) { //Make a new layer for targets
//You hit a target!
DeselectTarget(); //Deselect the old target
selectedTarget = hit.transform;
SelectTarget(); //Select the new target
gotTarget = true; //Set that we hit something
break; //Break out because we don't need to check anymore
}
}
if( !gotTarget ) DeselectTarget(); //If we missed everything, deselect
}
} }
Any help altering this would be appreciated i am programming C# and unity says there is and error NullReferenceException: Object reference not set to an instance of an object Targeting.DeselectTarget () (at Assets/Scripts for game/Targeting.cs:76) Targeting.Update () (at Assets/Scripts for game/Targeting.cs:100) so any help would appreciated thanks
Bump I'd like to address this issue myself... Did you get this script to work. can anyone out there lend a hand on the mouse select to target enemy issue?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Targeting by clicking on them 2 Answers
Enemy Select 1 Answer
What is wrong with my Code? (MouseDown event doesn't work) 1 Answer
Selecting enemies with mouse 0 Answers