[Solved] How to fill a water bottle via script ?

Hi everyone !

I’m trying to fill a water bottle using a button : When I press the button, the bottle is filled up gradually.
The bottle is represented by a slider and the water level by its value.

But i’m struggling making it work. I tried using Event Trigger component + while loop but it’s making Unity crash ^_^’

So i’m asking for help here after a few hours of ripping my hair off :confused:

Thanks by advance !

Does your current code make Unity crash or hang?

Event Trigger + while loop makes me think you are running a while loop on the UI thread, which would make the app appear to hang.

If that’s the case, look up coroutines and use one.

  1. Implement the IPointerDownHandler and IPointerUpHandler interfaces : Redirect to... title of new-page
  2. Set a boolean to true in the IPointerDown function, and to false in the other
  3. In the Update function, increase the value of the slider if the boolean is equal to true.
  4. Make sure your camera has a GraphicRaycaster component attached

I found what was wrong with my code !

It was like that :

public class WaterFilling : MonoBehaviour {

    public Slider WaterFill;
    public bool activ = false;

    private void Update()
    {
        while (activ == true)
        {
            WaterFill.value = WaterFill.value + 1;
        }

    }

    public void WFill()
    {
        activ = true;
    }
    public void StopFilling()
    {
        activ = false;
    }
    
}

With two event triggers : On pointer down → WFill & On pointer up → StopFilling

And I fixed it by replacing the while loop with an If :

public class WaterFilling : MonoBehaviour {

    public Slider WaterFill;
    public bool activ = false;

    private void Update()
    {
        if (activ == true)
        {
            WaterFill.value = WaterFill.value + 1;
        }

    }

    public void WFill()
    {
        activ = true;
    }
    public void StopFilling()
    {
        activ = false;
    }
    
}

So, yeah that was pretty simple but that’s because i’m new to Unity and coding in general
Thanks to those who answered !