Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Djspun · Sep 27, 2013 at 04:55 PM · c#newtargeting

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();
 }
 
 

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Djspun · Sep 27, 2013 at 09:04 PM 0
Share

i just want the script to re search for Enemy there gotta be something easier i dont want to re wright a code

avatar image
1

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);
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Djspun · Sep 27, 2013 at 09:05 PM 0
Share

i just want the script to re search for Enemy there gotta be something easier i dont want to re wright a code

avatar image whydoidoit · Sep 27, 2013 at 09:08 PM 0
Share

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.

avatar image whydoidoit · Sep 27, 2013 at 09:24 PM 0
Share
 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.

avatar image Djspun · Sep 27, 2013 at 10:45 PM 0
Share

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

avatar image whydoidoit · Sep 28, 2013 at 04:05 AM 0
Share

I guess you didn't include the Enemy class from my answer....

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges