- Home /
Question by
AmarKalabic · Feb 06, 2015 at 07:56 AM ·
javascriptinputscriptingbasics
Digging technique counter
Hello, I'm making and game which has digging as it's feature so I need timer which will count exact float (in seconds) and then destroy gameObject. This is what I tried now but it's freezing unity:
function Update()
{
if (Input.GetMouseButtonDown(0))
{
digTime = 1.5; // in secounds
}
while (!Input.GetMouseButtonUp(0)) // why is this infinite loop?
{
digtime -= Time.deltaTime;
if (digtime <= 0)
{
Destroy(hit.collider.gameObject);
}
}
Comment
http://rbwhitaker.wikidot.com/c-sharp-tutorials
I'd strongly suggest spending some time getting familiar with C# fundamentals. A while loop executes forever until the condition is false. You have created a situation here where the condition to break the loop can never happen. Thus, infinite loop. :)
Answer by abi-kr01 · Feb 06, 2015 at 11:13 AM
if you want a simple timer then you can use this
function Update()
{
if (Input.GetMouseButtonDown(0))
{
starttimer();
}
}
starttimer()
{\\ this will run for 20 sec don't go for syntax as no monodevelop suggestion here :)
for(int i=0;i<20;i++)
{
yield return waitforsec(1);
if(i==19)
{
destroy(this.gameobject);
}
}
}