- Home /
Script crashes unity
When ever i have this script active and try to run the game, it crashes Unity. I've got no idea why so can someone please explain:
var walkSpeed : float = 1.0; var runSpeed : float = 2.0; var gridSize : int = 10;
function Start () { var myTransform = transform; var startPosition : Vector3; var endPosition : Vector3; var t : float; var tx : float; var moveSpeed = walkSpeed; while(true) { if(Input.GetButtonDown("Fire1")){ var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit;
if (Physics.Raycast(ray,hit)) {
startPosition = myTransform.position;
endx = hit.transform.position.x/2;
endz = hit.transform.position.z/2;
endPosition = Vector3(endx,0,endz);
while (t < 1.0) {
moveSpeed = Input.GetButton("Run")? runSpeed : walkSpeed;
t += Time.deltaTime * (moveSpeed/gridSize);
myTransform.position = Vector3.Lerp(startPosition, endPosition, t);
yield;
}
}
tx = t - 1.0; // Used to prevent slight visual hiccups on "grid lines" due to Time.deltaTime variance
}
}
}
I find with bugs that cause editor crashes the best thing to do first is to do a standalone build then run that and perform whatever action was crashing your game in the standalone build. A lot of the time the game won't crash in the standalone build. You can then check the log file and get a more useful error message that will help you narrow down the problem.
Answer by Eric5h5 · Aug 22, 2010 at 02:56 AM
You have an infinite loop. It's checking for the fire button being down an infinite number of times in one frame. I assume you want a yield in there.
Answer by Ben 14 · Oct 04, 2010 at 06:33 PM
Eric5h5 has it, but I don't think you can yield inside Start(). Try either firing another function and do the yield in there, or even more simply move the content of your while(true) to the Update() function which will be called on each frame.
Start can yield. In C# you'd have to declare it as an IEnumerator but javascript does it automatically. The issue above is the while(true) that never has a break.
Your answer

Follow this Question
Related Questions
A node in a childnode? 1 Answer
Why does Unity 3.0 fail to start on OS X (Mac) 3 Answers
Unity3d Crash error (Mono.dll) with NetworkView 0 Answers
Unity + dk2 compilation problem 1 Answer
Profiler crashes unity 0 Answers