- Home /
Stuttering / performance issue with certain JS scripts
Working on a very simple pong style game, I have two scripts which cause the game to skip / stutter for the first few seconds of the scenes loading up. If I remove these two scripts, the stuttering goes away. It seems to stabilize after 10-15 seconds and only happens when the level first starts. However, since this game is physics based and the ball is always moving, any skipping, stuttering or slowdown is visible.
I am seeing the stuttering issue on an iPhone 5 but it is not visible on the engine Game window. Using the free version of Unity, I do not have access to the profiler.
Any ideas why these two bits of script are causing stuttering issues?
Thank you
This is a countdown timer. When the timer reaches zero, the game advances to the next level.
//Countdown Timer (Visible)
var endTime : float;
var textMesh : TextMesh;
function Start()
{
endTime = Time.time + 63;
textMesh = GameObject.Find ("Text_Timer").GetComponent(TextMesh);
textMesh.text = "63";
}
function Update()
{
var timeLeft : int = endTime - Time.time;
if (timeLeft < 0) timeLeft = 0;
textMesh.text = timeLeft.ToString();
}
//End level and load win screen after X Seconds (Invisible)
var timeLimit : float = 63; // 1 minute timer
Invoke("NextMap", timeLimit);
function NextMap()
{
Application.LoadLevel("Win");
}
This is a quit game button which takes the player back to the main menu.
private var ray : Ray;
private var rayCastHit : RaycastHit;
//Load Previous button
function LateUpdate ()
{
//Load Previous Level
if(Input.GetMouseButtonDown(0))
{
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, rayCastHit))
{
if(rayCastHit.transform.name == "Text QuitToMenu")
{
Application.LoadLevel("L0.0 Menu");
}
}
}
}
I think garbage collection mechanism works to clear the previous level cache you might want to check the profiler when running on iphone to clarify the real problem.You should also use CoRoutines for time checking ins$$anonymous$$d of looking up for it every frame and you might want to try loadAsync
Thank you for the info. Unfortunately, I am using the free version of Unity, I do not have access to the profiler. Nor does the free version make loadAsync available.
I have gotten rid of the quit game button and simplified the countdown timer to the following. Yet it still hitches at exactly the same spots every time. No matter when I start the timer, the game hitches at 54 and 53 seconds on the countdown.
Here is my updated/simplified countdown timer.
var timer: float = 60; // set duration time in seconds in the Inspector
function Update()
{
timer -= Time.deltaTime; // I need timer which from a particular time goes to zero
guiText.text = timer.ToString("F0");
}
Answer by Knoxley · Jul 11, 2014 at 09:47 PM
Turns out, it was the 3D Text turning on and off. It was creating a texture right then that was causing the game to pause momentarily. I solved this by adding those letting in a preload area of the game.