Good Evening All!
I’ve got some code here that is not having any compiler errors, but it’s not functioning correctly. I am able to tab target (somewhat reliably), however, my text stating my distance from the target will either function if I manually drag my target into the inspector, or give me a “AnotherTransform has not been assigned”. This makes sense as I can physically see it empty, but I’ve tried to give it a null setting, I’ve tried to wait until I can tab target, and none of them seem to work unless I put a specific object in there first. So I get tab targeting, but my distance is always to the object I placed in there in the inspector. From “private void ShowSelectedTarget” down is really where I think the issue is that I can’t seem to mix around any which way with any success. Any and all help would be most appreciated!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class targeting : MonoBehaviour
{
public Transform OneTransform;
public Transform AnotherTransform;
public UnityEngine.UI.Text distLabel; // <-- Assign the Text element in the Inspector view.
public List<Transform> targets;
public Transform selectedTarget;
public string targetTag = "Targetable";
private Transform myTransform;
void Start ()
{
targets = new List<Transform>();
selectedTarget = null;
myTransform = transform;
AddAllTargetables();
}
public void AddAllTargetables()
{
GameObject[] go = GameObject.FindGameObjectsWithTag("Targetable");
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));
});
}
private void TargetTargetable()
{
if(selectedTarget == null)
{
SortTargetsByDistance();
selectedTarget = targets[0];
}
//// EDIT: Add this code; now we can jump 2 all the found enemy's by pressing TAB
else
{
int index = targets.IndexOf(selectedTarget);
// because it starts at 0 so that's acually already 1
if (index < targets.Count -1)
{
index++;
}
// if we are at the LAST enemy in ar found list, jump back to the first enemy in the list
else
{
index = 0;
}
ShowDeSelectedTarget();
selectedTarget = targets[index];
}
ShowSelectedTarget();
}
private void ShowSelectedTarget()
{
Transform AnotherTransform = selectedTarget.FindChild ("Name");
Transform name = selectedTarget.FindChild ("Name");
if (name == null) {
Debug.LogError ("Could not find the Name on " + selectedTarget.name);
return;
}
name.GetComponent<TextMesh> ().text = selectedTarget.GetComponent<ring>().name;
name.GetComponent<MeshRenderer> ().enabled = true;
}
private void ShowDeSelectedTarget()
{
selectedTarget.FindChild("Name").GetComponent<MeshRenderer> ().enabled = false;
// name.GetComponent<MeshRenderer> ().enabled = false;
selectedTarget = null;
}
void Update ()
{
if (OneTransform) {
// Transform AnotherTransform = selectedTarget;
float dist = Vector3.Distance(OneTransform.position, AnotherTransform.position);
distLabel.text = dist.ToString("Tgt: 0.0ft");
}
if(Input.GetKeyDown(KeyCode.Tab))
{
Transform AnotherTransform = selectedTarget;
TargetTargetable();
}
}
}