iOS Optimisation Questions "Invoke" "StartCoroutine" "Update"

Hello,
well now i’m left the design stage of my project and now it comes to optimization…
There are so different opinions and approaches that can be really confusing…
So here is my first Question :
I have a Update function in my Script that looks like :

function Update() {
	if (!SpaceShip)
	SpaceShip = GameObject.FindWithTag("SpaceCraft");
	if(SpaceShip) {
	if (pause == false){
	UpdateAttitude (SpaceShip.transform); //Richtungszeiger
	HealthLevelerTransform.rotation.eulerAngles.z = 90.0 + SpaceShip.transform.rotation.eulerAngles.z;
	ShipRotX = 0.5 + SpaceShip.transform.rotation.x; //Neigung
	RadarCompassTransform.rotation.eulerAngles.z = SpaceShip.transform.rotation.eulerAngles.y;
	var hit : RaycastHit;
    if (Physics.Raycast (SpaceShip.transform.position, -Vector3.up, hit)) {
    GroundDistance = hit.distance;
    if (GroundDistance < 10.0) {
    GroundWarning = true;
    } else {
    GroundWarning = false;
    }
	}
}
	if (! Cam1)
	Cam1 = GameObject.FindWithTag ("MainCamera");
	if (!Cam1Audio)
	Cam1Audio = Cam1.GetComponentInChildren(AudioListener);
	if (! Cam3)
	Cam3 = GameObject.Find ("GUI");
	if (!Cam2)
	Cam2 = SpaceShip.GetComponentInChildren(Camera);
	//Cam2 = GameObject.FindWithTag("ShakeCam").GetComponent(Camera);
	if (!Cam2Audio)
	Cam2Audio = SpaceShip.GetComponentInChildren(AudioListener);
        if (! ShipLight)
	ShipLight = SpaceShip.Find("Pointlight");
	if (! Follow)
	Follow = SpaceShip.GetComponentInChildren(FollowTransform);

So because i Spawn my Player i have this GO find stuff… but that’s only @ beginning of the Game…
I hope i can change it later and connect it with my Spawn Pool so that there is no GO find function…
But for the Rest it looks to me that it doesn’t have to be in a update function…
So i did this :

	public var targettingFrequency : float = 1.2f;
	public var trajectoryFrequency : float = 0.2f;
	public var trajectoryBlend : float = 1.0f;
	private var updatedTrajectory : Vector3 = Vector3.zero;
	private var currentTrajectory : Vector3  = Vector3.zero;

function Start (){
	InvokeRepeating("DoTargetting", targettingFrequency*Random.value, targettingFrequency);
	InvokeRepeating("DoTrajectory", trajectoryFrequency*Random.value, trajectoryFrequency);
	DoTargetting();
	DoTrajectory(); }

function Update () {
currentTrajectory = Vector3.Lerp(currentTrajectory, updatedTrajectory, Time.deltaTime * trajectoryBlend);
	radarRotation.z = radarRotation.z + rotateSpeed * Time.deltaTime;
	RadarZeigerTransform.rotation.eulerAngles.z = radarRotation.z;
}
function DoTargetting() {
		// search for targets here, we don't do it often because it's expensive
	if (!SpaceShip)
	SpaceShip = GameObject.FindWithTag("SpaceCraft");
      	if (! Cam1)
	Cam1 = GameObject.FindWithTag ("MainCamera");
	if (!Cam1Audio)
	Cam1Audio = Cam1.GetComponentInChildren(AudioListener);
	if (! Cam3)
	Cam3 = GameObject.Find ("GUI");
	if (!Cam2)
	Cam2 = SpaceShip.GetComponentInChildren(Camera);
	//Cam2 = GameObject.FindWithTag("ShakeCam").GetComponent(Camera);
	if (!Cam2Audio)
	Cam2Audio = SpaceShip.GetComponentInChildren(AudioListener);
	if (! ShipLight)
	ShipLight = SpaceShip.Find("Pointlight");
	if (! Follow)
	Follow = SpaceShip.GetComponentInChildren(FollowTransform);
}

function DoTrajectory() {
		// trajectory isn't as expensive but it's not something we need to do every frame
	if(SpaceShip) {
	if (pause == false){
	UpdateAttitude (SpaceShip.transform); //Richtungszeiger
	HealthLevelerTransform.rotation.eulerAngles.z = 90.0 + SpaceShip.transform.rotation.eulerAngles.z;
	ShipRotX = 0.5 + SpaceShip.transform.rotation.x; //Neigung
	RadarCompassTransform.rotation.eulerAngles.z = SpaceShip.transform.rotation.eulerAngles.y;
    var hit : RaycastHit;
    if (Physics.Raycast (SpaceShip.transform.position, -Vector3.up, hit)) {
    GroundDistance = hit.distance;
    if (GroundDistance < 10.0) {
    GroundWarning = true;
    } else {
    GroundWarning = false;
    }
	}
}	
}

My Question is : Makes this optimization sense ? There is no big difference in the fps if i do that.
Is invoke Repeating the same as StartCoroutine ? Where is the difference ? The Doc’s are not really helpful and if you look @ the AngryBots demo
there is like noooo Optimization… all is done in Update also Raycast and simple Texture Animation… So i think i can’t learn from it how to optimize my Game…
Also many People here say (maybe Wrong):

function DoSomething (someParameter : float) {
    while (true) {
        print("DoSomething Loop");
        // Yield execution of this coroutine and return to the main loop until next frame
        yield;
    }
}

The Coroutine should be better for Performance than Update… that’s not true i think if you run it only with yield… than it run’s also every frame and has the same perf. impact as a Update function or ?

So thank you to take the time to lighten me up what the best Performance way is…

It’s incredibly minimal ie nothing gained from using invoke from a main loop. There are only measurable gains when using it for say, enemies which do ai. For example there may be 30+ enemies in a level, and you’ll want them to call their hunt and scan functions at regular intervals.

When its just a few things like 1-2 textures or whatever… dude you’re not going to optimise it using co routines. Pick your battles. They’re perfect for when you have a lot of things that need to be called less than every frame. Not just a few things :slight_smile:

First, move all yours FindWithTag, Find and GetComponent in Start() or Awake(), no need to call them in any loop if you don’t need to change them constantly. Always cache your reference, you’ll avoid a lot of costly calls.

Then consider that if you are targeting 3GS+ you’ll see an impact on performance if you have a ton of Update() (around 100 or more, depends on complexity), hundreds of raycasts per frames and so on, optimizinig is always good, but you’ll not going to see a tangibile impact on a script on a single object (like your main player), it becomes relevand if there are tens of them active at the same time. So for a complex game it’s the rule. :wink:

Thank you !!!
Now i feel better :slight_smile: and i know what to do !
thank you very much for reply !!!