TransWikia.com

Is there an easy way to get out of this IF loop?

Stack Overflow Asked by Matias on January 7, 2021

I’m learning how to program and I feel that I’m always trapped with this kind of loops problems.
the question is, what would be the best way to get out of the if when the grouped bool is always true on Update (every frame). I need to execute the EUREKA only once.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GroupedWordsEyes : MonoBehaviour

{
    void Update()
    {
        //Check if words are grouped 
        bool grouped = CompareTags_Word1.colliding == true;

        if (grouped)
        {
            Debug.Log("EUREKAAAA!");
            //get out of the loop!
        }
    }
}

3 Answers

It sounds like you want that Update is only called until the condition matches the first time.

You can simply disable that component by setting enabled to false.

This way Update is no longer called by Unity.

public class GroupedWordsEyes : MonoBehaviour   
{
    void Update()
    {
        //Check if words are grouped 
        if (CompareTags_Word1.colliding)
        {
            Debug.Log("EUREKAAAA!");
            
            enabled = false;
        }
    }
}

Answered by derHugo on January 7, 2021

It seems like you want the code within the if to execute only when "colliding" changes. Then you need to remember the previous value and check for changes:

EDIT changed code based on comments by OP

public class GroupedWordsEyes : MonoBehaviour
{
    private bool previousGrouped = false; // assume that at first there is no collision

    void Update()
    {
        //Check if words are grouped 
        bool grouped = CompareTags_Word1.colliding == true;

        if (grouped != previousGrouped )
        {
            // always remember a change
            previousGrouped  = grouped;

            // only when just collided
            if (grouped)
            {
              Debug.Log("EUREKAAAA!");
            }
        }
    }
}

Answered by Hans Kesting on January 7, 2021

EDIT: Updating answer as I misunderstood the question.

You've got several options and it entirely depends on how you've architected.

  1. One option is to deactivate (or even completely destory) the component. After you've done the one thing you need to do, call this.enabled = false - this will disable the component and no scripts will be run. This depends on architecture as this will also disable any other functions or child components attached to this one. You can mitigate this by ensuring this script only contains specific pieces that you want to be able to disable.

  2. Create a flag that indicates whether the action has already been done.

    if (grouped && !hasDoneEureka)
    {
        // do the thing
        hasDoneEureka = true;
    }
    

Note that this will ensure you only run what you need to once, but probably isn't optimal since while it won't "do the thing" every time, it will CHECK every frame.

Answered by McAden on January 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP