Trouble switching to 2nd object in list.
I have 1 game object ring1 with the tag "ring" and many copies of the prefab hoop1 with the tag "hoop". I want for my RingScript is attached to ring1 and HoopScript is attached to hoop1. I want for my ring1 to go to the nearest hoop1 object,destroy it,and move on to the next hoop1 object. The following script manages to get to the first hoop1 object,but it doesn't switch it's SelecteTarget for another hoop1 object. Any help or suggestions would be welcome. using UnityEngine; using System.Collections; using System.Collections.Generic;
public class RingScript : MonoBehaviour
{
public Vector3 loc;
public float speed;
public Vector3 scale;
public List<Transform> Enemies;
public Transform SelectedTarget;
void Start()
{
SelectedTarget = null;
Enemies = new List<Transform>();
AddEnemiesToList();
}
public void AddEnemiesToList()
{
GameObject[] ItemsInList = GameObject.FindGameObjectsWithTag("hoop");
foreach (GameObject hoop1 in ItemsInList)
{
Enemies.Add(hoop1.transform);
}
}
public void DistanceToTarget()
{
Enemies.Sort(delegate (Transform t1, Transform t2)
{
return Vector3.Distance(t1.transform.position, transform.position).CompareTo(Vector3.Distance(t2.transform.position, transform.position));
});
}
public void TargetedEnemy()
{
if (SelectedTarget == null)
{
SelectedTarget = Enemies[0];
}
}
void Update()
{
TargetedEnemy();
transform.position = Vector3.MoveTowards(transform.position, SelectedTarget.position, speed * Time.deltaTime);
}
}
using UnityEngine;
using System.Collections;
public class HoopScript : MonoBehaviour
{
public Vector3 destroyloc;
public Transform t1;
public Vector3 hooploc;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
destroyloc = GameObject.FindGameObjectWithTag("ring").transform.position;
if (destroyloc == transform.position)
{
// Destroy(gameObject);
gameObject.SetActive(false);
}
}
}
Your answer
Follow this Question
Related Questions
How I Sort a List at Start?,Sorting a List on Start 0 Answers
Sort list by distance AND by being active 3 Answers
Why can't I find a GameObject with FindGameObjectsWithTag 2 Answers
How do i move a list object at the end of the list while shifting back the others? 1 Answer
Sort a List with a public class 0 Answers