• 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
0
Question by matthew_tavares1 · May 22, 2012 at 08:01 PM · c#tagsattack

Player can no longer attack enemies with one is destroyed.

I've made it so that my player can successfully attack and kill an enemy however if one enemy is killed he can no longer target the others, since he found the enemy by a tag. How can I make it so that he can continue to fight other enemies? Here's my script: using UnityEngine; using System.Collections;

public class CatchJellyFish : MonoBehaviour { public float Timer; public float coolDownSwing;

 public GameObject jellyFish;
 
 
 public void Start()
 {
     Timer = 0;
     coolDownSwing = 1.0f;
     
     jellyFish = GameObject.FindGameObjectsWithTag("JellyFish");
 }

 public void Update()
 {
     if (Timer > 0)
         Timer -= Time.deltaTime;

     if (Timer < 0)
         Timer = 0;
     
     float distance = Vector3.Distance(jellyFish.transform.position, transform.position);

     if(Input.GetButton("Action1") && Time.timeScale != 0)
     {            if (Timer == 0)
         {
             Timer = coolDownSwing;
             if(distance < 8f)
             {
                 JellyFishHealth jellyHealth = (JellyFishHealth)jellyFish.GetComponent("JellyFishHealth");
                    jellyHealth.AdjustCurrentHealth(-1);
             }
         }
     }
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by BiG · May 22, 2012 at 08:09 PM

First, you are assigning jellyFish's value in the Start function (so, at the beginning of the game), and you aren't going to update it in the future. So, if new enemies are generated, jellyFish variable remains useless, because its value won't change.

As a second point, FindGameObjectsWithTag returns a list: this means that you have to search, in that list, for the particular enemy that's closest to the player.

As a solution for both the problems, update jellyFish in the Update function, and use a routine that finds, during time, the closest enemy. In the Script Reference, there's an example that does exactly that.

Comment
Add comment · Show 6 · 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 matthew_tavares1 · May 22, 2012 at 10:12 PM 0
Share

I did everything that you recommended, but I keep get an error when I use unity's example at the return closest, saying that it's never assigned, even though I copied it straight from their site.

avatar image BiG · May 22, 2012 at 10:23 PM 0
Share

Copying things "straight" is not always the right approach. As an example, had you changed the tag "Enemy" from the reference script with the tag "JellyFish" (the one that you are using)? I hope that's the only thing "misaligned".

avatar image matthew_tavares1 · May 22, 2012 at 10:31 PM 0
Share

Yeah, I changed it so my code looks somewhat like this now

public class CatchJellyFish : $$anonymous$$onoBehaviour { public float Timer; public float coolDownSwing;

 public GameObject jellyFish;
 
 GameObject FindClosestJellyFish()
 {
     GameObject[] gos;
     gos = GameObject.FindGameObjectsWithTag("JellyFish");
     GameObject closest;
     float distance = $$anonymous$$athf.Infinity;
     Vector3 position = transform.position;
     foreach (GameObject go in gos)
     {
         Vector3 diff = go.transform.position - position;
         float curDistance = diff.sqr$$anonymous$$agnitude;
         if (curDistance < distance)
         {
             closest = go;
             distance = curDistance;
         }
     }
     return closest;
 }
avatar image BiG · May 22, 2012 at 10:34 PM 0
Share

O$$anonymous$$... Stupid question: are you actually calling this function in Update()? $$anonymous$$aybe you've just declared that, and you forgot to call it.

avatar image matthew_tavares1 · May 22, 2012 at 10:48 PM 0
Share

I'm sorry I'm feeling pretty dumb for not getting this. Here's how my code is right now, it's no longer giving me an error when I kill an enemy but it's randomly selecting which ones I can and cannot attack.

public class CatchJellyFish : $$anonymous$$onoBehaviour { public float Timer; public float coolDownSwing;

 public GameObject jellyFish;
 
 GameObject FindClosestJellyFish()
 {
     GameObject[] gos;
     gos = GameObject.FindGameObjectsWithTag("JellyFish");
     GameObject closest = null;
     float distance = $$anonymous$$athf.Infinity;
     Vector3 position = transform.position;
     foreach (GameObject go in gos)
     {
         Vector3 diff = go.transform.position - position;
         float curDistance = diff.sqr$$anonymous$$agnitude;
         if (curDistance < distance)
         {
             closest = go;
             distance = curDistance;
         }
     }
     return closest;
 }
 
 public void Start()
 {
     Timer = 0;
     coolDownSwing = 1.0f;
 }

 public void Update()
 {
     FindClosestJellyFish();
         
     if (Timer > 0)
         Timer -= Time.deltaTime;

     if (Timer < 0)
         Timer = 0;
     
     jellyFish = GameObject.FindGameObjectWithTag("JellyFish");
     
     float distance = Vector3.Distance(jellyFish.transform.position, transform.position);
     
     if(Input.GetButton("Action1") && Time.timeScale != 0)
     {            if (Timer == 0)
         {
             Timer = coolDownSwing;
             if(distance < 8f)
             {
                 JellyFishHealth jellyHealth = (JellyFishHealth)jellyFish.GetComponent("JellyFishHealth");
                    jellyHealth.AdjustCurrentHealth(-1);
             }
         }
     }
 }

}

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Is there an opposite of GameObject.FindGameObjectsWithTag() ? 2 Answers

deactivated script firing 0 Answers

C# Enemy list for player to attack 2D 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