- Home /
c# how to refresh my targeting list
my targeting script gets a list of enemies i can target but once i kill one the script dose not update the list and says missing game object making it impossible to target and attack and kill the other enemies
here is my targeting script
/// /// TargetMob.cs /// This script can be attached to any permanent gameobject, and is responsible for allowing the player to target different mobs that are with in range /// using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Targetting : 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));
});
}
//if we do not have an enemy targeted ywt, then find the clostest one and target him
//if we do have an enemy targeted, then get the next target
//if we have the last target in the list, then get then first target in the list
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();
}
}
Answer by PProductions · Sep 27, 2013 at 05:36 PM
I would suggest having the script that controls your enemy send a message to the TargetMob script when they die. I expect the Mob script will already have a reference to the player so I suggest attaching the TargetMob script to the player object. Then, in the Mob script:
player.SendMessage("RemoveTarget", transform);
(Obviously replacing 'player' with whatever your variable is called.
Then, in TargetMob add a new method like this:
public void RemoveTarget(Transform target) {
targets.RemoveAt(targets.IndexOf(target));
}
I hope this solves your problem
i just want the script to re search for Enemy there gotta be something easier i dont want to re wright a code
Answer by whydoidoit · Sep 27, 2013 at 05:39 PM
You'd be better off doing that in a different way. Rather than finding the enemies like you do, attach a script to each enemy that records it in an available list and removes itself when destroyed.
Here is my base class I use for things like this:
using System.Collections.Generic;
using UnityEngine;
public class MonoCollection<T> : MonoBehaviour where T : MonoCollection<T>
{
public static List<T> all = new List<T>();
protected virtual void OnEnableEx()
{
}
protected virtual void OnDisableEx()
{
}
protected void OnEnable()
{
all.Add(this as T);
OnEnableEx();
}
protected void OnDisable()
{
all.Remove(this as T);
OnDisableEx();
}
}
So lets say you make an Enemy class
public class Enemy : MonoCollection<Enemy>
{
public string type;
//Presuming you want functionality in OnEnable write an override
//of OnEnableEx() or call the base function
new void OnEnable()
{
base.OnEnable(); // Make sure to call the base if you write your own OnEnable
}
}
Now you can get your enemies like this:
foreach(var enemy in Enemy.all)
Note that if you are sorting based on distance your method is significanly slower than using Linq and OrderBy. This is due to the fact that .Sort needs to call the predicate for each comparison which uses as Sqrt in your case, Linq only calls each one once. The fastest way to write that sort is:
using System.Linq;
...
var myPosition = transform.position;
var sortedOrder = Enemy.all.OrderBy(e=>Vector3.Distance(e.transform.position, myPosition); //.ToArray() or .ToList() if you need them all
var closest = sortedOrder.FirstOrDefault();
Now it's even faster if you write a Linq extension for MinBy, then it's just a single pass through the list:
public static T MinBy<T,TR>(this IEnumerable<T> source, Func<T, TR> clause, IComparer<TR> comparer=null)
{
T result = default(T);
TR min = default(TR);
bool doneOne;
IComparer<TR> = Comparer<TR>.Default;
foreach(var item in source)
{
var current = clause(item);
if(!doneOne || comparer.Compare(min, current) > 0)
{
min=current;
result = item;
doneOne = true;
}
}
return result;
}
Now you can do:
var myPosition = transform.position;
var closest = Enemy.all.MinBy(e=>Vector3.Distance(e.transform.position, myPosition);
i just want the script to re search for Enemy there gotta be something easier i dont want to re wright a code
Well as I've given you all the code you need, it wouldn't be much work. It would also be a great deal faster. But it's up to you.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Targetting : $$anonymous$$onoBehaviour {
public Transform selectedTarget;
private Transform myTransform;
// Use this for initialization
void Start () {
selectedTarget = null;
myTransform = transform;
}
//if we do not have an enemy targeted ywt, then find the clostest one and target him
//if we do have an enemy targeted, then get the next target
//if we have the last target in the list, then get then first target in the list
private void TargetEnemy() {
var sortedEnemies = Enemy.all.OrderBy(e=>Vector3.Distance(e.transform.position, transform.position).ToList();
if(selectedTarget == null) {
selectedTarget = sortedEnemies.FirstOrDefault();
}
else {
int index = sortedList.IndexOf(selectedTarget);
if(index < sortedList.Count - 1) {
index++;
}
else {
index = 0;
}
DeselectTarget();
selectedTarget = sortedList[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.Get$$anonymous$$eyDown($$anonymous$$eyCode.Tab))
TargetEnemy();
}
Then rather than using a tag for your enemies, attach the enemy script from my answer. And include the $$anonymous$$onoCollection class in some file somewhere.
im getting a bunch of errors
Assets/_Scripts/Targetting.cs(28,25): error CS0103: The name Enemy' does not exist in the current context Assets/_Scripts/Targetting.cs(31,40): error CS0411: The type arguments for method
System.Linq.Enumerable.FirstOrDefault(this System.Collections.Generic.IEnumerable)' cannot be inferred from the usage. Try specifying the type arguments explicitly
Assets/_Scripts/Targetting.cs(35,20): error CS0103: The name sortedList' does not exist in the current context Assets/_Scripts/Targetting.cs(37,19): error CS0103: The name
sortedList' does not exist in the current context
Assets/_Scripts/Targetting.cs(44,25): error CS0103: The name `sortedList' does not exist in the current context
I guess you didn't include the Enemy class from my answer....
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
how can i auto target Gameobject on startup 1 Answer
c# how to get to idle from walk 0 Answers
Destroy Gameobject once 0 health 2 Answers
i need help removing an error in my targetting/attack script 1 Answer