Z
Z
Zimaell2020-10-23 17:00:31
Unity
Zimaell, 2020-10-23 17:00:31

What can reset the values ​​of variables?

I threw out everything superfluous from the script and left only the debug and this is what I saw

spoiler
public class Cell : MonoBehaviour {
  public int X, Y;
  public Move = false;
  void OnMouseDown(){
    Move = true;
    }
void Update(){
    if(Move) Debug.Log(X + "," + Y);
    }
}
#-----------------------------------------------------
public class Board : MonoBehaviour {
  public Dictionary<(int, int), GameObject> Cells = new Dictionary<(int, int), GameObject>();
  void Start(){
    CreateBoard();
    }

  void CreateBoard(){
    for(int y = 0; y < 5; y++){
      for(int x = 0; x < 5; x++){
        GameObject PrefabResources = Resources.Load("Prefabs/test", typeof(GameObject)) as GameObject;
        Cells[(x, y)] = Instantiate(PrefabResources, new Vector3(x, y, 0), Quaternion.identity) as GameObject;
        Cells[(x, y)].AddComponent<Cell>();
        Cells[(x, y)].GetComponent<Cell>().X = x;
        Cells[(x, y)].GetComponent<Cell>().Y = y;
        }
      }
    }
}

And in the end, when I click on some object, I get the following debug, for example

3.5
0.0
3.5
0.0
3.5
0.0
............

but after all, these variables should not change, in this case 3.5 should always be displayed, in Cell nowhere is 0 assigned to these variables, objects are lined up once and once they are assigned values, why can this be? (nothing is attached to prefabs initially)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question