Can't figure how to code it.

So there’s this thing I whant to code tried multiple time and either it does work or unity freeze when I play test it.

I created an array of input field and I whant to call a method inside the Update only of all the array are not focused. If one element of the array is focused I do t what the method to work/be call.

Seem simple but I can’t wrap my mind around it…

Thanks all for your generous help.

What have you tried so far?

first of all in the script where I have my methode I created a bool variable and in the update I called

 void Update()
    {
        if (isControl == true)
        {
           // method that take input control from numpad
            method();
        }
    }

then I tried to create a script that look like this:

public class InputFieldFocus : MonoBehaviour
{
    public InputField[] inputFields;

    void Update()
    {
        foreach (InputField element in inputFields)
        {
            while(element.isFocused)
            {
                ShipRBControler.instance.isControl = false;
                break;
            }
        }
    }
}

but when I run that unity freeze.

I also tried to put a piece of script directly on every input field that look like this but that didn;t work.

 void Update()
    {
        if (input.isFocused == true)
        {
            ShipRBControler.instance.isControl = false;
        }

        else if(input.isFocused == false)
        {
            ShipRBControler.instance.isControl = true;
            input.text = slider.value + "";
        }
    }

also I romeved both script and turned the bool on and off on the editor and I saw that this worked. I just can’t figured of to turn it on and off via script.

it seem so simple but I can’t seem to find the solution.

thanks mate.

Note: I have 5 input fields

I think your second attempt is really close! You just need to use an “if” instead of a while. The while will run forever which is causing your freeze:

public class InputFieldFocus : MonoBehaviour
{
    public InputField[] inputFields;
    void Update()
    {
        foreach (InputField element in inputFields)
        {
            if(element.isFocused)
            {
                ShipRBControler.instance.isControl = false;
                break;
            }
        }
    }
}

will try it live.

It’s so stupid! It was that.

Just added that piece of code to turn it on again:

 else if (element.isFocused == false)
            {
                ShipRBControler.instance.isControl = true;
            }

thanks mate. can’t belive I past 3h to search for one word…

since I’m a noob I have a question. is it good that the script that check for the array is a different script, or should I put that piece of script in the same scrip where my method is??

Both work, ultimately it comes down to preference for organization, making a structure that is clear to you. I prefer a big main script that does almost everything, and other scripts refer to it as needed. But connections between scripts can be done in all sorts of ways, and large scripts can get unwieldy, so there are tradeoffs. Making things modular is usually a good idea, but that can also go too far, where you have tons of functions or scripts that each do very little.

It’s a big discussion…

ok so for performence it doesn’t change much? it’s just how I prefere to handle it. thanks.

If there’s a performance tradeoff I’d bet it’s magnitudes less than other optimizations with rendering etc.

1 Like