Can anyone help me improve my code.

Hello.
I make a laser shooting game. Camera moves itself on a way. And i stuck on optimizing process. I write script, that rotate my camera to 1 of 4 angle and instantiate bots. But sometimes i see in the profiler spikes with this script. The original code contains about 500 lines of code. I’ve heard that instantiate a heavy function and in its place is better on and off gameObjects. But I do not know how it can be implemented. Here is the part of code:

#pragma strict
private var rndView : float;
static var angle_x : float;
static var angle_y : float;
var Bot_PM : GameObject;
var Bot_AK : GameObject;
var Bot_PM_UP : GameObject;
var Bot_AK_UP : GameObject;
var Bot_PM_ShootUP : GameObject;
var Bot_AK_ShootUP : GameObject;
var Bot_PM_ShootDown : GameObject;
var Bot_AK_ShootDown : GameObject;
var Hostage_Stand : GameObject;
var Hostage_Run : GameObject;
private var s_point_1 : Transform;
private var s_point_2 : Transform;
private var s_point_3 : Transform;
var S_Points : Transform[];
private var thingToMake : GameObject;
private var rndS : int;
private var rndPoint : int;
private var Hostage : GameObject;
private var location : Transform;
private var randomPick : int;
private var rndBot : int;

private var b_Spawn_at_1 : boolean;
private var b_Spawn_at_2 : boolean;
private var b_Spawn_at_3 : boolean;
var Aux_Points : Transform[];
var Hostage_Points : Transform[];
private var hostage_location : Transform;

function Start () {
b_Spawn_at_1 = false;
b_Spawn_at_2 = false;
b_Spawn_at_3 = false;
}

function Update () {
// Simply say if all terrorists killed in view, change view and spawns new bots.
// Maybe there is better to use coroutine? or invoke function? 
if(Terrorist_Counter_Level_3.b_Spawn){
b_Spawn_at_1 = false;
b_Spawn_at_2 = false;
b_Spawn_at_3 = false;
ChangeView();
Check();
Spawn();
if(CamMov_Level_3.b_InPos_3 || CamMov_Level_3.b_InPos_4 || CamMov_Level_3.b_InPos_7 || CamMov_Level_3.b_InPos_8){
	SpawnHostage_Stand();
}
if(CamMov_Level_3.b_InPos_1 || CamMov_Level_3.b_InPos_5 || CamMov_Level_3.b_InPos_9){
	SpawnHostage_Run();
}
Terrorist_Counter_Level_3.b_Spawn = false;
Terrorist_Counter_Level_3.b_can_Run = false;
}

if(Terrorist_Counter_Level_3.b_can_move){
transform.position = Camera.mainCamera.transform.position;
	}
}

//Here I check what position the player is and what angles can rotate the camera. I left here one position to not take up much space
function ChangeView(){
	rndView = Random.Range(1,3);
	if(CamMov_Level_3.b_InPos_3 || CamMov_Level_3.b_InPos_7)
	{
		if(rndView == 1)
		{	
			if(angle_y != 328){
				angle_x = 0;
				angle_y = 328;
			}
			else
			{
				angle_x = 0;
				angle_y = 238;
			}
		}
	if(rndView == 2)
		{	
			if(angle_y != 238){
				angle_x = 0;
				angle_y = 238;
			}
			else
			{
				angle_x = 0;
				angle_y = 328;
			}
		}
	}	
		
		RndRotation();
}

function RndRotation() {
	transform.eulerAngles = Vector3(angle_x,angle_y,0);	
}

//Here I check what position the player is and what bot must be instantiated. There i left one position, other is similar.

function Spawn(){

for (var i : int = 0; i < Terrorist_Counter_Level_3.rndN; i++)
{
 rndBot = Random.Range(1,3);
	
	if(CamMov_Level_3.b_InPos_3 || CamMov_Level_3.b_InPos_4 || CamMov_Level_3.b_InPos_7 || CamMov_Level_3.b_InPos_8){	
		
	randomPick  = Random.Range(1,4); 
	
	if(randomPick == 1){
	if(!b_Spawn_at_1){
		location = s_point_1;
		b_Spawn_at_1 = true;		
		}
	else if(!b_Spawn_at_2){
		location = s_point_2;
		b_Spawn_at_2 = true;
		}
	else if(!b_Spawn_at_3){
		location = s_point_3;
		b_Spawn_at_3 = true;
		}
	}
	
	if(randomPick == 2){
	if(!b_Spawn_at_2){
		location = s_point_2;
		b_Spawn_at_2 = true;		
		}
	else if(!b_Spawn_at_1){
		location = s_point_1;
		b_Spawn_at_1 = true;
		}
	else if(!b_Spawn_at_3){
		location = s_point_3;
		b_Spawn_at_3 = true;
		}
	}
	
	if(randomPick == 3){
	if(!b_Spawn_at_3){
		location = s_point_3;
		b_Spawn_at_3 = true;		
		}
	else if(!b_Spawn_at_2){
		location = s_point_2;
		b_Spawn_at_2 = true;
		}
	else if(!b_Spawn_at_1){
		location = s_point_1;
		b_Spawn_at_1 = true;
		}
	}
		
		if(rndBot == 1){
			thingToMake = Instantiate(Bot_AK_UP,location.position, location.rotation);
		}
		if(rndBot == 2){
			thingToMake = Instantiate(Bot_PM_UP,location.position, location.rotation);						
		}
	}
}


// Here i check in which position player and assign spawn points.
function Check()
{

if(CamMov_Level_3.b_InPos_3)
	{	
	if(angle_y == 238  angle_x == 0){
		s_point_1 = S_Points[8];
		s_point_2 = S_Points[9];
		s_point_3 = Aux_Points[0];
		hostage_location = Hostage_Points[0];
		}
	if(angle_y == 328  angle_x == 0){
		s_point_1 = S_Points[10];
		s_point_2 = S_Points[11];
		s_point_3 = Aux_Points[1];
		hostage_location = Hostage_Points[1];
		}
	}
}


function SpawnHostage_Stand()
{
	Hostage = Instantiate(Hostage_Stand,hostage_location.position, hostage_location.rotation);
}

function SpawnHostage_Run()
{
	Hostage = Instantiate(Hostage_Run,location.position, location.rotation);
}

I hope you will help me make my code better. Thanks in advice.

Before you optimize your code you should first run it on your target platform. If you are able to run it smoothly then it doesn’t need to be optimized even if the profiler goes crazy. If your game is crashing or slowing down then start to look through your update functions an try and optimize there. I’ll take a look at your code and see what I can find.

Thanks. My frame rate is about 100 on my 2-core 2,3 GHz computer and 5670 ati radeon videocard. The problem is that the game must run on 3 and more monitors and i assume that my fps is greatly fall. Sometimes freezes appear in the game when executes:

if(Terrorist_Counter_Level_3.b_Spawn){

b_Spawn_at_1 = false;
b_Spawn_at_2 = false;
b_Spawn_at_3 = false;

ChangeView();
Check();
Spawn();

if(CamMov_Level_3.b_InPos_3 || CamMov_Level_3.b_InPos_4 || CamMov_Level_3.b_InPos_7 || CamMov_Level_3.b_InPos_8){
    SpawnHostage_Stand();
}

if(CamMov_Level_3.b_InPos_1 || CamMov_Level_3.b_InPos_5 || CamMov_Level_3.b_InPos_9){
    SpawnHostage_Run();
}

Terrorist_Counter_Level_3.b_Spawn = false;
Terrorist_Counter_Level_3.b_can_Run = false;
}

Sometimes Instantiate took 17 and more percent of CPU usage. And how can i reasigned colliders size for my prefab? Now i used script that take size of colliders from textfile and assigned them to all new instantiated bot. So when bot is instantiated it take values from txt file and assigned them to colliders. And sometimes profiler says, that it was created garbage collection with this script ( i dont really now what is garbage collection, but i know that is bad). So can i assigned values from txt file to the prefab, instead assigned them to gameObject?

I think your biggest problem is the multiple if checks. I am not 100 percent sure what it is your script is fully doing, but I know having your game run through that many checks can cause performance issues. You should use an array to hold multiple numbers rather than multiple variables. This way you can just call the index of the array rather than having to take up memory for each variable. Instead of instantiate what you want to look for is object pooling your characters. You can find a couple of examples online or there is scripts in the asset store to help you with this. Your slow down most likely is coming from you Instantiating all your characters in your for loop from Spawn() then you Instantiate in your update for the hostages. Since you are causing the game to spawn all these things at once it is causing a hiccup in your game. Try the object pooling like I said or try and stagger your Instantiate calls out a little bit more through a co routine.

if(Terrorist_Counter_Level_3.b_Spawn){

 

b_Spawn_at_1 = false;

b_Spawn_at_2 = false;

b_Spawn_at_3 = false;

 

ChangeView();

Check();

Spawn();

// **** put wait here ****
WaitForSeconds(0.25f);
 

if(CamMov_Level_3.b_InPos_3 || CamMov_Level_3.b_InPos_4 || CamMov_Level_3.b_InPos_7 || CamMov_Level_3.b_InPos_8){

    SpawnHostage_Stand();

}

 

if(CamMov_Level_3.b_InPos_1 || CamMov_Level_3.b_InPos_5 || CamMov_Level_3.b_InPos_9){

    SpawnHostage_Run();

}

 

Terrorist_Counter_Level_3.b_Spawn = false;

Terrorist_Counter_Level_3.b_can_Run = false;

}

That’s just an example, but you can also add a wait for seconds in your for loop as well.

thanks for answer. I will try to deal with pooling system. I can show you video with my game, which help you better understand how my game works.

http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html

These are especially fun! :smile:

InvokeRepeating(). CancelInvoke().

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.InvokeRepeating.html

Is coroutine really can make my code better?

InvokeRepeating() can precisely time the Instantiate of the gameObjects
You use coroutine’s when methods like Start(), Update(), FixedUpdate(),. is used.
Unity works better when you follow “it’s way of doing it?”, guidelines, dunno if that that answered the question? anyway only one way to find out :smile:

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.StartCoroutine.html

Thanks. ok, i will try coroutine.