- Home /
How to cycle through targets?
I am trying to make it to where when TAB is pressed, target is equal to targets[0], then targets[1], targets[2] (However long the array is) then repeat back from 0.
And also having trouble showing a texture icon to show which one is targeted currently. It shows up but farrrrr from the actual target.
here is what i have so far.
using UnityEngine;
using System.Collections;
public class T1_TargetSystem : MonoBehaviour
{
public GameObject[] targets;
private int currentTarget;
private int totalTargets;
public Transform target;
public Texture2D image;
Vector3 point;
void Start()
{
}
void Update()
{
target = targets[currentTarget];
if (currentTarget == targets.Length)
{
currentTarget = 0;
}
targets = GameObject.FindGameObjectsWithTag("Enemy");
if (currentTarget == totalTargets)
{
return;
}
else
{
// Find screen position for target
point = Camera.main.WorldToScreenPoint(target.position);
if (Input.GetKeyDown(KeyCode.Tab))
{
currentTarget++;
}
}
}
void OnGUI()
{
Rect rect = new Rect(point.x, point.y, 20, 20);
}
}
For cycling:
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Tab))
{
currentTarget = (currentTarget + 1) % targets.Length;
}
I am having an error with targets[](GameObject) to target(Transform)
Answer by SirCrazyNugget · Mar 24, 2014 at 03:12 PM
//only create the array once (or when new targets are added)
void Start(){
targets = GameObject.FindGameObjectsWithTag("Enemy");
}
void Update(){
if(Input.GetKeyDown(KeyCode.Tab)){
currentTarget = currentTarget + 1 % targets.Length;
target = targets[currentTarget];
}
}
Why not just place an object in the 3D space above the intended target rather than trying to convert world space to screen space?
I was wanting some sort of visual icon over the targets. So if they are behind a wall or hill I can still see their target icon such as a red triangle image around them if they are targeted
cannot complicity convert unityengine.gameobject to unityengine.transform
Yea hes saying you could attach whatever target icon to the actual object and enanle/disable and even rotate them towards the players if needed rather then figuring out a world space thing.
oh okay alot better idea. But still having issues getting the cycle through targets to work
Answer by JoshMBeyer · Mar 24, 2014 at 06:28 PM
I got it to get rid of errors buy adding this but its not doing anything at all. no throws to console or anything
if (Input.GetKeyDown(KeyCode.Tab))
{
currentTarget = currentTarget + 1 % targets.Length;
target = targets[currentTarget].transform;
}
saying t1_targetsystem.totaltargets is never assigned to and will always have value of 0??
Added this but same results, no errors but still doesn't do anything.
void Start()
{
totalTargets = targets.Length;
}
http://www.youtube.com/watch?v=zNuCvWnGdXA
Telling you bro, this is almost exactly what you want targeting wise $$anonymous$$us the texture icon.
Your answer
Follow this Question
Related Questions
Get list of nearby targets (JS) 1 Answer
Projectile Selection through Array 1 Answer
Array Help 2 Answers
cycle trhu an Array of gameobjects? 1 Answer
Make a Button For Every String in an Array of Level Names 1 Answer