- Home /
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
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 things by keeping track of them from the beginning.
Thanks smog,you're absolutely correct. $$anonymous$$y 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
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)
Answer by guto-thomas · Aug 08, 2014 at 10:03 PM
You might want to use coroutines and it would be something like this:
void Start ()
{
StartCoroutine (MoveAndDestroy ());
}
IEnumerator MoveToTarget (GameObject t)
{
Vector3 dir = (t.transform.position - transform.position).normalized;
while (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
Thanks $$anonymous$$, this helped a lot. I've got the script working, but I have a few questions:
How can I control the speed at which my character moves between targets. Currently, he moves lightning fast.
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 : $$anonymous$$onoBehaviour {
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
Follow this Question
Related Questions
What am I doing wrong with this collision code? 4 Answers
How to find layer instead of tags 3 Answers
Destroy all objects with tag "enemy"? 2 Answers
FindGameObjectsWithTag check gameObject disabled? 1 Answer
GameObjectWithTag Child 1 Answer