Jaun Samtin
Jaun Samtin

Reputation: 15

Do I interpret "bad looping" code correctly?

I made it a habit to program "looping" code in the manner shown bellow. I am a self taught coder, and I am just wondering if it is really necessary or not. (This example is in Unity C#):

Example: Will this:

void Update()
{
 //GameOver Procedure

       if(shipCurrentHP <= 0 && GameOver == false)
        {
            StartCoroutine(BlowUp());
            GameOver = true;
        }
}

Be any better than this?:

void Update()
{
     //GameOver Procedure

        if(shipCurrentHP <= 0)
        {
           StartCoroutine(BlowUp());
           GameOver = true;
        }
}

Upvotes: 0

Views: 95

Answers (1)

zambari
zambari

Reputation: 5035

Your second example will start another coroutine in every frame after the condidion is initially met, it will most likely just eat up all the RAM of the machine if the user does nothing after that point. Generally doing stuff in Update should be avoided unless you are doing somethign that actually needs to execute every frame

Upvotes: 1

Related Questions