the variables zeroX, zeroY take on certain values ​​in the Start () function, they do not change anywhere else. but when the clickButtonNum () function is called, their values ​​are not the same as those resulting from Start (), what could be the problem?

public class UI_Game : MonoBehaviour { public GameObject button, imageParent; GameObject[,] matrixButtons = new GameObject[4, 4]; int[,] modelMatrix = new int[4, 4]; private int zeroX; private int zeroY; void Start () // срабатывает при создании объекта { generateMap (); } private void generateMap() //генерирует поле для игры { int[] num = new int[16]; for (int a = 0; a != 16; a++) //массив с числами для создания модели поля { num [a] = a; } mixArray (num); int index = 0; for (int x = 0; x!=4;x++) { for (int y = 0; y != 4; y++) { if (num [index] != 0) { createButton (x,y,num [index]); modelMatrix [x, y] = num [index]; //запоминаем положение номеров кнопок в модель } else { modelMatrix [x, y] = num [index]; zeroX = x; zeroY = y; } index++; } } } private void createButton(int x, int y, int num) // создает кнопку-фишку с нужным числом в нужных коорднатах { GameObject buttonLink = Instantiate (button); buttonLink.transform.SetParent (imageParent.transform); Vector3 newPosition = new Vector3 (-60 + x * 40, -60 + y * 40); buttonLink.transform.localPosition = newPosition; buttonLink.transform.Find ("Text").GetComponent<UnityEngine.UI.Text> ().text = num.ToString(); buttonLink.transform.GetComponent<Coordinates_button> ().set_x (x); buttonLink.transform.GetComponent<Coordinates_button> ().set_y (y); matrixButtons [x, y] = buttonLink; } private void mixArray(int [] num) // перемешивает числа в массиве { int swapNum = Random.Range (10, 20); for (int a = 0; a != swapNum; a++) { int firstNum = Random.Range (0, 16); int secondNum = Random.Range (0, 16); int bufer = num [firstNum]; num [firstNum] = num [secondNum]; num [secondNum] = bufer; } } public void clickButtonNum() // обратывает нажатие по кнопке-фишке { Debug.Log (zeroX); 
  • "the meaning is ... not what is needed" - what is needed and what turns out to be - should we guess? - Igor
  • Numbers What numbers are there? and what do you expect? - Igor
  • Well, random numbers from 0 to 3 inclusive are expected, for example, zeroX recorded the value 1, when I use it, it is always 0, for zeroY it is always 3 - Anton Simakov

1 answer 1

Copied your script to yourself. In Awake, I generated a map, in Start I bring zeroX and zeroY to the console - every time they are different, which means everything works correctly. Those. at the time of creation, they are definitely not always 0 and 3. Apparently, you, nevertheless, change them somewhere later. Select the variable and press Shift + F12 - it will show you all the places where it is used.