MaD
MaD

Reputation: 21

Call a fuction every time a bool is true

i have a gameobject that goes up if it stays in collision for 5 Secs, the problem is it only work once , i tried calling the OnGUI in the update when ever the ToggleGUI = true but did't work

public float elapsedTime = 0f;
bool ToggleGUI = false;
bool isCreated = false;
Vector3 firstpos;


private void Update()
{
    if(ToggleGUI == true)
    {
        OnGUI();
    }
}

void OnTriggerStay(Collider other)
{
    
        elapsedTime += Time.deltaTime;
        
        if (elapsedTime >= 5.0f)
        {
            ToggleGUI = true;
        }
    
}

void OnTriggerExit(Collider other)
{
    elapsedTime = 0f;
}

void OnGUI()
{
    if (ToggleGUI == true)
    {
        if (!isCreated)
        {
            firstpos = transform.position;

            firstpos.y += 2f;

            transform.position = firstpos;

            isCreated = true;
        }
    }
}

Upvotes: 0

Views: 56

Answers (2)

derHugo
derHugo

Reputation: 90580

Your code seems a bit to complex for what you are trying to achieve.

If following your description what you want is just that the object goes up by 2f units every time you have stayed within a collider for 5 seconds I would simply use a Corouinte like e.g.

// The currently running Coroutine
private Coroutine _routine;

private int currentTriggers;

private void OnTriggerEnter(Collider other)
{
    currentTriggers++;

    // Start a routine if none is running already
    if(_routine == null)
    {
        _routine = StartCorouine (Routine());
    }
}

private void OnTriggerExit(Collider other)
{
    currentTriggers--;

    // If there are still other triggers do nothing
    if(currentTriggers > 0) return;

    // Cancel the routine if one is running
    if(_routine != null)
    {
        StopCoroutine(_routine);
        _routine = null;
    }
}

IEnumerator Routine()
{   
    // As the name says wait 5 seconds
    yield return new WaitForSeconds (5f);

    // This is only reached if the routine wasn't stopped before the time passed
   
    // Then move your object up     
    transform.position += Vector3.up * 2f;

    _routine = null;
}

In general be very careful with your method names!

OnGUI is a special built-in event message that is called for handling events such as Keyboard or mouse input events, Repaint calls from the UI, etc. It might get called multiple times within a single frame and is absolutely not what you want to do here and for sure you should not call this method "manually" from Update.

Upvotes: 0

Gabriele Stramandino
Gabriele Stramandino

Reputation: 11

after isCreated is set to true there is no place in your code where that variable is set again to false, therefore it can get inside the condition that moves the gameobject just once for the lifecycle of the script.

FYI when you pass a bool to an if you don't need to write ==true to have it execute when that variable is true, the name of the variable is enough

Upvotes: 1

Related Questions