- Home /
More effeciant way to select targets? rather then GameObject.FindGameObjectsWithTag?
hey guys so my Airplane game runs fine, but when My AI chooses to attack a target it lags hard, then runs fine after a Target is selected any wayto do this more efficiantly? So I guess when it runns though all the tagged game objects its takes alot more processing power and slows down the game :)
TargetTs = GameObject.FindGameObjectsWithTag("TeamTwo");
index = Random.Range(0, TargetTs.Length);
TargetT = TargetTs[index];
With around 20+ AI slows the game down, but works fine when they have their targets :)
void FixedUpdate()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
if (AttackingNow == false)
{
AttackTimeCoolDown -= 1 * Time.deltaTime;
if(AttackTimeCoolDown < 0)
{
AttackTimeCoolDown = AttackTimeCoolDownReset;
AttackingNow = true;
Debug.Log("Attacking Now...");
}
if (TargetT == null)
{
TargetTs = GameObject.FindGameObjectsWithTag("Point");
index = Random.Range(0, TargetTs.Length);
TargetT = TargetTs[index];
}
if (TargetT != null)
{
Distance = Vector3.Distance(TargetT.transform.position, transform.position);
PathFinding();
if (Distance < 7)
{
TargetT = null;
}
}
}
if (AttackingNow == true)
{
PathFinding();
AttackTime -= 1 * Time.deltaTime;
TargetTs = GameObject.FindGameObjectsWithTag("TeamTwo");
index = Random.Range(0, TargetTs.Length);
TargetT = TargetTs[index];
if (AttackTime < 0)
{
AttackTime = AttackTimeReset;
AttackingNow = false;
Debug.Log(" Now...");
}
}
}
Can you cache the TargetTs and only update it when it's modified? Also, have you tried using the built-in unity profiling tool?
Hey buddy thanks for they reply, tohught I was only doing an update when it was modified, (when the plane its attacking is null, searcdh for a new plane :) ) How do you cash things I prety much try to do simple code in unty myself :)
simply cache the instance once at the start of the game and not in the update for every instance, only once for all instances
Your answer
Follow this Question
Related Questions
How can I compare a sequence of GameObjects when tapped? 1 Answer
Object modified in script not modified in scene 0 Answers
How i get my cloud position in other script? I want to use this temp value in other script. 1 Answer
"left is not set up when it is set up under input. / Axes / horizontal...." 1 Answer
Fading in and out gui text possible? 1 Answer