Really slow ai

Hi,
i have tryed to create some basic ai for the spacefighters in my game.
Everything works fine if there are only a few fighters in the scene, but if there are lets say around 60 of them it slows down the game extremely.
I think there are performence Problems in the ai script, but downt know how to make it faster. Can anyone help me to optimice my code?

Dont be to hard, its my very first game in unity 3d.
Thank you in Advance.

    using UnityEngine;
using System.Collections;


public class ai2 : MonoBehaviour {
public float damping = (float) 5;
public float damping2 = (float) 2;
private GameObject[] targets;
private GameObject[] goings;
private GameObject[] togoings;
public Transform currenttarget;
public Transform gun;
private float timer = 0;
private float timer2 = 0;
private float timer3 = 2;
private Transform navpoint;
private bool ok = false;
private bool landing = false;
private bool landingflight = false;
public static bool landingcommand = false;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void FixedUpdate (){
	
	if (ok == false && landing == false){starting ();}
	if (ok == true){
	Findenemy();}
	try{
	if (ok == true && currenttarget.tag == "enemyfighter"){
	fly ();
	}
	else{
		if( ok == true && currenttarget.tag == "Enemy"){
		fly2 ();}}}catch{currenttarget = Targeting.fighterTarget;}
	if (landingcommand == true && ok == true) {landingflight = true;}
	if (landingflight == true){
	gotonav();	
	}
	if(landing == true){
		land();
	}
	shoot ();
	
			
				
				
			
		}


void attack(){
		if (currenttarget != null){
	Quaternion rotation = Quaternion.LookRotation(currenttarget.position - transform.position);
			transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
			timer = timer + Time.deltaTime * Random.value;
			
		}
			if (currenttarget == null){
				currenttarget = Targeting.fighterTarget;
			}
	
		
}
	void Findenemy(){
		targets = GameObject.FindGameObjectsWithTag("enemyfighter");
		
foreach (GameObject target in targets){
		Quaternion neededrotation = Quaternion.LookRotation(target.transform.position - transform.position);
		float allrotneed = Mathf.Abs(neededrotation.eulerAngles.x) +  Mathf.Abs(neededrotation.eulerAngles.y) +  Mathf.Abs(neededrotation.eulerAngles.z);
		float allrot = Mathf.Infinity;
			if (allrotneed < allrot) {
			currenttarget = target.transform;
				allrot = allrotneed;
				if (currenttarget == null){currenttarget = Targeting.fighterTarget;}
	}
}
}
	void fly(){
	try{
			
			if (Vector3.Distance(currenttarget.position,transform.position) < 200){
		transform.Translate(Vector3.forward * Time.deltaTime * 460);
			
		}
		if (Vector3.Distance(currenttarget.position,transform.position) > 200){
			if (timer < 3){
		if (ok == true) {attack ();}
			}
			if (timer > 3){
				Vector3 evade = new Vector3 (Random.Range (-90,90) ,Random.Range (-90,90) ,Random.Range (-90,90) );
				Quaternion rotation = Quaternion.LookRotation(evade - transform.position);
				transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping2);
				
					transform.Translate(Vector3.forward * Time.deltaTime * 420);
					timer2 = timer2 + Time.deltaTime  * Random.value;
				if (timer2 > (float) 0.5) {timer = 0; timer2 = 0;}
			}
			transform.Translate(Vector3.forward * Time.deltaTime * 400);
		
		}
		}
		catch{
		currenttarget = Targeting.fighterTarget;	
		}
	}
	
	void starting(){
		goings = GameObject.FindGameObjectsWithTag("hangarnav");
		
		float distance = Mathf.Infinity;
		Vector3 position = transform.position;
		foreach (GameObject go in goings){
		Vector3 diff = (go.transform.position - position);
		float curdist = diff.sqrMagnitude;
			if (curdist < distance)  {
			navpoint = go.transform;
				distance = curdist;
			}
			if (distance >= 100){
			transform.LookAt (navpoint);
				transform.Translate(Vector3.forward * Time.deltaTime * 460);
		
			}
			
			if (distance < 100)
			{
			ok = true;	
			
			}
		}
	}
	void gotonav(){
		ok = false;
		GameObject[] goings = GameObject.FindGameObjectsWithTag("hangarnav");
		float distance = Mathf.Infinity;
		Vector3 position = transform.position;
		foreach (GameObject go in goings){
		Vector3 diff = (go.transform.position - position);
		float curdist = diff.sqrMagnitude;
			if (curdist < distance)  {
			navpoint = go.transform;
				distance = curdist;
			}
		}
			if (distance >= 40){
			Quaternion rotation = Quaternion.LookRotation(navpoint.position - transform.position);
			transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * (float) 0.2);
				transform.Translate(Vector3.forward * Time.deltaTime * 300);
				
			}
			
			if (distance < 40)
			{
				landing = true;
			landingflight = false;
			
			}
	}
	void land(){
		ok = false;
		GameObject[] goings = GameObject.FindGameObjectsWithTag("hangar");
		float distance = Mathf.Infinity;
		Vector3 position = transform.position;
		foreach (GameObject go in goings){
		Vector3 diff = (go.transform.position - position);
		float curdist = diff.sqrMagnitude;
			if (curdist < distance)  {
			navpoint = go.transform;
				distance = curdist;
			}
		}
			if (distance >= 50){
			Quaternion rotation = Quaternion.LookRotation(navpoint.position - transform.position);
			transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 5);
				transform.Translate(Vector3.forward * Time.deltaTime * 300);
				
			}
			
			if (distance < 100)
			{
			
			hangar.fighters = hangar.fighters + 1;
			ok = true;
				Destroy (gameObject);
			
			}
	}
	
void fly2(){
		
	try{
			currenttarget = Targeting.fighterTarget;
			if (Vector3.Distance(currenttarget.position,transform.position) < 900){
	
				Quaternion rotation = Quaternion.Euler(Random.Range (-45,45),Random.Range (-45,45),90);
				transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping2);
			transform.Translate(Vector3.forward * Time.deltaTime * 400);
				timer3 = timer3 - Time.deltaTime;
		}
		if (Vector3.Distance(currenttarget.position,transform.position) > 900){
				timer3 = timer3 +Time.deltaTime;
	if(timer3 > 1){
		attack ();
			timer3 = 2;
				}
			transform.Translate(Vector3.forward * Time.deltaTime * 400);
		
		}
		}
		catch{
		currenttarget = Targeting.fighterTarget;	
		}
	}
	void shoot(){
		try{
	if (currenttarget.tag == "enemyfighter" && Vector3.Distance (currenttarget.position,transform.position) < 700){fightershoot.shoot =true;}
	if (currenttarget.tag == "enemyfighter" && Vector3.Distance (currenttarget.position,transform.position) > 700){fightershoot.shoot =false;}
	if (currenttarget.tag == "Enemy" && Vector3.Distance (currenttarget.position,transform.position) < 1200){fightershoot.shoot2 =true;}
	if (currenttarget.tag == "Enemy" && Vector3.Distance (currenttarget.position,transform.position) > 1200){fightershoot.shoot2 =false;}
	if (currenttarget.tag == "Enemy" && Vector3.Distance (currenttarget.position,transform.position) < 1200){fightershoot.shoot =true;}
	if (currenttarget.tag == "Enemy" && Vector3.Distance (currenttarget.position,transform.position) > 1200){fightershoot.shoot =false;}
		}catch{fightershoot.shoot = false;}
	}
}

I’m guessing you’re not a pro user so can’t use the profiler?

On a very brief look through - calling FindGameObjectsWithTag too often will definitely cause you problems. Better to either find them all at the start of the game and store the list somewhere (assuming they don’t add/remove as you go), or have a manager class which each thing you’re looking for registers itself with when it’s created (and de-registers when destroyed). Either way you’ll have a ready-made list each time you want to find something, rather than the horribly slow FindGameObjectsWithTag.

Aside from that, consider whether all of them need to do a full update every frame, or if you could just update (say) 5 each frame. There might be some stuff you need to do each frame (like moving them), but then the more complex stuff (looking for enemies, etc.) that each get away with doing once a second or so.

Hope that helps!

Rob.

i found the problem.
it wasnt in the script…
every fighter had a camera attached to it (for fighter perspective).
so thre were around 50 cameras in the scene…
this seems to be deadly for the game speed…