- Home /
Script is causing immense lag, and I don't know what's causing it.
I have zombies in a game that fire mini projectiles at players, and have health scripts as well. I know it's one of their scripts because adding them to the game causes it to quickly become laggy, and then unplayable.
this is the HP script,
var HP : int = 5;
var body : GameObject;
var speed : int = 5;
function Start () {
body = gameObject;
}
function Update () {
transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
if(HP < 1) {
var instance : GameObject = Instantiate(body, transform.position, transform.rotation);
DestroyObject (gameObject);
}
}
function OnCollisionEnter (myCollision : Collision) {
if(myCollision.gameObject.name == "zombieclimb"){
transform.Translate(Vector3(0,1,0) * Time.deltaTime);
}
}
and the shooting script,
var projectile : GameObject;
var startdelay = 0.1;
var delay = 0.1;
InvokeRepeating("LaunchProjectile", startdelay, delay);
function LaunchProjectile () {
var instance : GameObject = Instantiate(projectile, transform.position, transform.rotation);
}
function Start () {
projectile = gameObject;
}
Again, I don't know what is causing it, but I suspect it may be the Update function. Also, nothing appears in the console either.
Answer by SeeSharp · May 04, 2014 at 10:19 PM
Hi,
If you start lagging after you shoot for a while, then add this piece of code to your 'LaunchProjectile' funtion: Destroy(instance, 10)
That will destroy the instantiated bullet after 10 seconds.
Other than that, everything looks fine to me and I cannot locate anything else that might lag your game right now.
Please ask if you need further assistance.
Best of luck,
SeeSharp.
I changed the script to this,
var projectile = gameObject;
var startdelay = 0.1;
var delay = 0.1;
InvokeRepeating("LaunchProjectile", startdelay, delay);
function LaunchProjectile () {
var instance : GameObject = Instantiate(projectile, transform.position, transform.rotation);
}
And it no longer lags. But it comes up with a new error,
UnityException: You are not allowed to call this function when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration. If you are using C# don't use this function in the constructor or field initializers, Ins$$anonymous$$d move initialization to the Awake or Start function. enemyshoot..ctor () (at Assets/scripts/enemyshoot.js:1)
It's why i changed it to the start function and went wrong, but I dont know how to fix it from there.
I got it, it reads as,
var projectile : GameObject;
var startdelay = 0.1;
var delay = 0.1;
InvokeRepeating("LaunchProjectile", startdelay, delay);
function LaunchProjectile () {
var instance : GameObject = Instantiate(projectile, transform.position, transform.rotation);
}
and works now.
Answer by Kiloblargh · May 04, 2014 at 10:28 PM
The InvokeRepeating() shouldn't even work, as it is outside a function. You can declare variables outside a function but not do anything else.
If it did work, it looks like the object with this "shooting script" on it would copy itself 10 times a second, and all of those copies would make copies of themselves 10 times a second, exponentially overflowing your scene with instances and probably crashing the game.
the delay variables are changed in the inspector, for most of the zombies, I have it set to a start delay of 0, and delay of 3.
And, you're right, I started the scene, and was able to pause it quickly enough to see that indeed it was spawning clones of itself.
Your answer
![](https://koobas.hobune.stream/wayback/20220613144245im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Incremental game need help 1 Answer
How to crouch without falling through floor? 2 Answers
Performance Optimization ~Function Update: Loop or Once ? 5 Answers
Trouble making money system 1 Answer