- Home /
FPS takes a hit when using my enemy health script
heres the code, whats costing so much its just a simple script?
#pragma strict
var Health : int;var Damage : int;var Gore : GameObject;var ME : GameObject;var DesME : GameObject;var IDied : boolean;
function OnTriggerEnter (col : Collider)
{
if (col.tag=="Hurt2")
{
Health -= Damage;
}
}
function Update()
{
if (Health <0)
{
Health += 50;
IDied = true;
}
if (IDied)
{
Instantiate(Gore, transform.position, transform.rotation);
Destroy(DesME);
}
}
Answer by Cuttlas-U · Sep 10, 2017 at 11:16 PM
hi; first of all i should say java script will be deleted from unity soon so try to switch to csharp;
to check tags do like this : if (col.CompareTag("Hurt2"))
this way is faster;
in update u created an Instantiate when id die this part will be called many times as the Die will be true and update calls many times per sec and the Instantiate is a huge performance cost , so make sure to force it to be done only once; (u can make another bool to control it );
another idea u dont need to put health check in update ; thats a big cost to check every frame for it ; u can put the check inside a function and only call the function to check when the trigger is happening;
i dont see any other problems in your script; good luck ;
Your answer
Follow this Question
Related Questions
Unity 2019.3 LWRP strange Performance Issue on android 0 Answers
Performance load of OnGUI 0 Answers
Problem with the performance when activating gameObject 0 Answers
Speedtree verts are insane 0 Answers