Todd Weiss Todd Weiss - 2 months ago 6
C# Question

while loop is not working the code isnt runnign 60 frames

hey guys i am new to coding and am trying to work this simple code out i need this code to run for for 5 seconds in a continuous loop without putting it into update.

void Update () {

}
void OnTriggerEnter2D(Collider2D other)
{
timer += Time.deltaTime;
if (other.gameObject.CompareTag("IcePickup"))
{
Destroy(other.gameObject);



Blade1 = GameObject.Find("Blade1");
Blade1.gameObject.GetComponent<SpriteRenderer>().sprite = IceBlade;

int counting = 1;
while (counting <= 10)
{
print("icey");
(Blade1.transform.Rotate(new Vector3(0, 0, -0.50f));
counting ++;
}


}

Answer

Well, maybe this example can help you:

using System;

class Program
{
    private static void Main(string[] args)
    {
        DateTime StartTime = DateTime.Now;
        int Seconds = 5;
        int Counter = 0;

        while(DateTime.Now - StartTime < TimeSpan.FromSeconds(Seconds))
        {
            //Your Code here?
            Counter++;
        }

        Console.WriteLine("In " + Seconds + "seconds are " + Counter + "iterations");
    }
}

Regards!