- Home /
GameObject.FindGameObjectsWithTag-low FPS
I use GameObject.FindGameobjectWithTag for finding enemies , but when i have many turrels with this script, fps is very low, i use this function in update(), but i heard that it needs to be in Awake(), but when I fill code in awake() turrels dont work I actually use Code FindClosestEnemy and write in c#. Thanks a lot.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class cannon : MonoBehaviour {
Transform target;
public Transform bullet;
public float attackRange = 30.0f;
public float shootAngleDistance = 10.0f;
public float gundamag = 30;
float _time = 5.0f;
double t = 0.3f;
float objectdefault_y_position = 21.55736f;
void Awake(){
}
void Start(){
}
void Update(){
GameObject closest=null;
float distance = 5000f;
foreach(var enemy in Enemy_test_Scrpt.AllEnemies)
{
var diff=(enemy.transform.position-transform.position);
var curDistance=diff.sqrMagnitude;
if(curDistance<distance)
{
closest=enemy;
distance=curDistance;
}
}
target=closest.transform;
var targetPoint = target.position;
var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 4.0f);
var forward = transform.TransformDirection(Vector3.forward);
var targetDir = target.position - transform.position;
RaycastHit hit;
if(Physics.Raycast(transform.position,transform.forward,out hit,200f)){//стрельба
Debug.DrawLine( transform.position, target.position, Color.red);
Pause ();
if(t>=0.5){
if(hit.transform.gameObject.layer!=4){
hit.transform.gameObject.GetComponent<Enemy_test_Scrpt>().target_health-=gundamag;
}
t=0.3;
}
}
}
void Pause(){
if(t<0.5){
t+=Time.deltaTime*(1.0/_time);
}
}
}
Next time please format your code in a way it can be read. You had around 4-6 empty lines between each line of code and nothing was indented in a reasonable way.
Ok, sorry. When I have 5-6 turrels at scene it works grat, but when ~30-50 it's so laggy
Answer by whydoidoit · Jun 09, 2012 at 06:14 PM
Yep don't go calling FindGameObjectWithTag in Update(). Add the code to Start() and cache the results in a list or an array.
Or add a script to your enemies that stores them in a list that is a static:
using System.Collections.Generic;
public class EnemyList : MonoBehaviour
{
public static List<GameObject> AllEnemies = new List<GameObject>();
void Awake()
{
AllEnemies.Add(gameObject);
}
void OnDestroy()
{
AllEnemies.Remove(gameObject);
}
}
Now you can work through your enemies using:
foreach(var enemy in EnemyList.AllEnemies)
{
}
The code I added to the answer enables enemies to be created during the game - you should use that rather than caching in Start().
Sorry, but I dont understand what I should do in foreach? Thanks.
Well you add that script to your enemies - now you have a List of game objects which is like an array. You can do with it whatever you did with your FindObjectsWithTag - but no need to find them.
You can do EnemyList.AllEnemies[0] etc. It's length is in EnemyList.AllEnemies.Count and foreach lets you run through the whole list.
Thanks it works, but where i should fill foreach? Into Update or Start?
Your answer
Follow this Question
Related Questions
Low FPS after loading a scene 1 Answer
Scroll rect on mobile? 0 Answers
low fps when looking in a direction with many objects 2 Answers
Blank scene, 2 sprites, 32 FPS 2 Answers
Serious performance issues 0 Answers