• 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 Stef · Aug 08, 2014 at 08:48 PM · destroytagfindnext

FindGameObjectWith "next" Tag

Hey Guys,

Problem: Finding one game object with tag "one", destroying it, and then finding a different game object with a different tag "two", and destroying that one. Then on to tag "three" and so on.

Here's what I have so far:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAI : MonoBehaviour {
     public Transform target;
     public int moveSpeed;
     public int rotationSpeed;
     public float maxDistance = .5f;
 
     private Transform myTransform;
     NavMeshAgent agent;
 
 
     void Awake () {
         myTransform = transform;
     }
 
 
     void Start () {
         agent = GetComponent<NavMeshAgent> ();
 
     }
     
 
     void Update () {
 
         GameObject go = GameObject.FindGameObjectWithTag("Tower");
         
         target = go.transform;
         Debug.DrawLine(target.position, myTransform.position, Color.yellow);
 
         //Look at Target
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
 
         if (Vector3.Distance(target.position, myTransform.position) > maxDistance) {
         //move towards Target
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     }
         if(Target == null)
         {
             GameObject go = GameObject.FindGameObjectWithTag("baseCenter");
         }
     }
 
 }

Any suggestions?

Thanks,

Stef

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
2

Answer by smoggach · Aug 08, 2014 at 09:16 PM

 Destroy(GameObject.FindGameObjectWithTag("one"));
 Destroy(GameObject.FindGameObjectWithTag("two"));
 Destroy(GameObject.FindGameObjectWithTag("three"));

You may want to clarify your question. You can avoid having to find t$$anonymous$$ngs by keeping track of them from the beginning.

Comment
Add comment · Show 2 · 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 Stef · Aug 08, 2014 at 09:28 PM 0
Share

Thanks smog,you're absolutely correct. My question is asked poorly. Let me rephrase. I want my character to from one game object to another based upon those objects tags. Once I arrive at the first object a timer starts that counts down and destroys the object. After that object is destroyed the character needs to be able to move to the next game object tagged "two" and have the same timer/destroy process occur... then onto tag "three" and so on. Hope this clarifies things. Any suggestions?

S

avatar image smoggach · Aug 11, 2014 at 01:39 PM 0
Share

I'd recommend reading up on data structures. U will probably have success using a List (for keeping track of things to destroy), a Singleton (to stay alive and perform the destruction), and the Invoke function (for destroying after a timer)

avatar image
1

Answer by guto-thomas · Aug 08, 2014 at 10:03 PM

You might want to use coroutines and it would be somet$$anonymous$$ng like t$$anonymous$$s:

 void Start ()
 {
     StartCoroutine (MoveAndDestroy ());
 }
 
 IEnumerator MoveToTarget (GameObject t)
 {
     Vector3 dir = (t.transform.position - transform.position).normalized;
 
     w$$anonymous$$le (Vector3.Distance (t.transform.position, transform.position) > 2)
     {
         transform.position += dir;
         yield return null;
     }
 }
 
 IEnumerator MoveAndDestroy ()
 {
     GameObject one = GameObject.FindGameObjectWithTag("one");
     GameObject two = GameObject.FindGameObjectWithTag ("two");
     GameObject three = GameObject.FindGameObjectWithTag ("three");
     yield return StartCoroutine (MoveToTarget (one));
     Destroy (one);
     yield return StartCoroutine (MoveToTarget (two));
     Destroy (two);
     yield return StartCoroutine (MoveToTarget (three));
     Destroy (three);
 }

Hope it helps.

Cheers,

Thomas

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 Stef · Aug 08, 2014 at 10:58 PM 0
Share

Thanks Thomas, this helped a lot. I've got the script working, but I have a few questions:

  1. How can I control the speed at which my character moves between targets. Currently, he moves lightning fast.

  2. How can I control the speed at which the target is destroyed. The game itself is a swarm of zombies attacking a group of buildings one at a time. When a zombie enters a buildings trigger a timer starts. When the timer reaches zero the building is destroyed. If there are two zombies in the trigger the timer should move twice as fast. So, the zombies need to stay at the building until it is destroyed, and then move onto the next building. I have a timer on the building that works fine:

    public class Strike_Timer : MonoBehaviour {

      public float countdown = 3.0f;
     
         void OnTriggerStay(Collider col) 
         {
             if(col.gameObject.tag == "Zombie")
                 countdown -= Time.deltaTime;
         }
     
         void Update ()
         {
             if(countdown <= 0.0f)
                 {
                     Destroy(gameObject);
                 }
         }
     }
    
    
    

Thoughts?...

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroy All Children of Object 6 Answers

character collider colliding with another character collider 1 Answer

Whats the best way to access other Game Objects? 2 Answers

GameObject.FindWithTag doesn't work! 1 Answer

How do I make Destroy(gameObject) work in my 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