- Home /
Timed while loop?
So Here is my code, now my problem is Unity is obviously designed to crash when there is an infinite loop, so how can I make a: 1. Timed loop, that goes off every .1 seconds, 2. would that still crash? if so how would I make it not? Heads up, this is the code for a machine gun.
var c1 : Color = Color.yellow;
var c2 : Color = Color.red;
var lineRenderer : LineRenderer;
var myPrefab = gameObject;
function Start() {
lineRenderer = gameObject.AddComponent(LineRenderer);
lineRenderer.material = new Material (Shader.Find("Particles/Additive"));
lineRenderer.SetColors(c1, c2);
lineRenderer.SetWidth(0.2,0.2);
lineRenderer.SetVertexCount(2);
}
function Update(){
var origin = transform.position;
var direction = transform.forward;
var endPoint = origin + direction * 100000;
var hit : RaycastHit;
lineRenderer.SetPosition(0, origin);
if (Input.GetButtonDown("Fire1")) {
while(Input.GetButtonDown("Fire1"))
{
if (Physics.Raycast(origin,direction,hit))
Instantiate(myPrefab, hit.point, Quaternion.identity);
endPoint = hit.point;
lineRenderer.SetPosition(1, endPoint);
}
}
}
it is extremely easy to do timers in Unity, for beginners.
just se Invoke or InvokeRepeating. there is a full intro at unityGE$$anonymous$$S.com
Since the Update function is called every frame, I think you should not put a "while" loop in it. Only a "if" statement will do the trick. No one wants an infinite loop in purpose :)
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Increment in while loop, with timeout 1 Answer
Yield Not Working - While Loop 3 Answers
while loop issue 1 Answer