0

I'm new to Unity and wants to make a Sudoku Solver. I've designed the input grid using input fields in the canvas. These input fields can display the numbers I generate in my grid script which will held the solving logic. My problem is that I want to update the input fields inside the loop to display(update all input fields to new value if there are one) when a value changes but can't get it right. Currently everything is displayed after the loop is done(in the next frame), but I need the values to update while the loop is running. Any ideas?

using System.Collections;
using System;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;
using System.Diagnostics;
using Debug = UnityEngine.Debug;

public class SudokuGrid : MonoBehaviour
{
    public InputField v00, v01,v02, v03, v04, v05, v06, v07, v08;

    private int[,] arr = new int[9, 9];

    public void Upd()
    {
        v00.text = arr[0, 0].ToString();
        v01.text = arr[0, 1].ToString();
        v02.text = arr[0, 2].ToString();
        v03.text = arr[0, 3].ToString();
        v04.text = arr[0, 4].ToString();
        v05.text = arr[0, 5].ToString();
        v06.text = arr[0, 6].ToString();
        v07.text = arr[0, 7].ToString();
        v08.text = arr[0, 8].ToString();

    }
    void Start()
    {
    }

    void Update()
    {

        int c = 1;
        for (var a = 0; a < 9; a++)
        {
            for (var b = 0; b < 9; b++)
            {
                arr[a, b]  = c;
                c++;
                Upd();
                //need to update all the inputfields here to display on screen
            }
        }      
    }
}
  • Please show your code (read also [mcve]) – UnholySheep Feb 16 '19 at 15:39
  • I'm still not exactly sure what your issue is - at what point exactly do you want to change the displayed numbers? Also this is C# code, not UnityScript – UnholySheep Feb 16 '19 at 15:53
  • The numbers is changed in the code but it does not change on the game screen. I want to be able to see how the code changes the inputfield values on the game screen the values us not updated to the game screen, only after the loop is finished evrything is displayed to the screen at once even if i put a sleep inside the loop all inputfield values change at the same time – Hanno kruger Feb 16 '19 at 16:00
  • Then you have to only update one value at a time in `Update` - the UI cannot render at the same time as `Update` is running – UnholySheep Feb 16 '19 at 16:01

2 Answers2

1

This is why Coroutines exist

void Start()
{
    StartCoroutine(RevealNumber());
}

IEnumerator RevealNumber()
{

    int c = 1;
    for (var a = 0; a < 9; a++)
    {
        for (var b = 0; b < 9; b++)
        {
            arr[a, b]  = c;
            c++;
            Upd();
            //waits for 1 second.
            yield return new WaitForSeconds(1);
        }
    }      
}
Community
  • 1
  • 1
0

Firstly, you can use 9x9 array of InputFields, its more readable

public InputField[,] InputFields;

Then you can update all the array using for loop:

for (int x = 0; x < 9; x++)
{
    for (int y = 0; y < 9; y++)
    {
        InputFields[x, y].text = arr[x, y];
    }
}      

OR you can detect user input in fields using on value change events to update only changed fields like this How to use the "On Value Change" in Unity3D Input Field UI component

caxapexac
  • 781
  • 9
  • 24
  • But how do you assign the 2darray values then in the inspector – Hanno kruger Feb 16 '19 at 18:53
  • Ouf, its easier to use List> (its visible in inspector) or you can read about 2d array in inspector https://answers.unity.com/questions/231715/is-there-any-way-to-view-2d-arrays-in-the-inspecto.html – caxapexac Feb 16 '19 at 18:59