Jonski Goldstein
Jonski Goldstein

Reputation: 169

Static array unity

I am trying to make a static array in unity that holds an integer in each element, however upon testing the array seems to be new for each object that accesses it.

static var tiles: float[];


function Start() {
  tiles = new float[6];
  tiles[1] = 1;
  Random();
}

function Random() {
  i = Random.Range(1, 6);
  if (i == 1) {
    tiles[1] ++;
  } else if (i == 2) {
    tiles[2] ++;
    print(tiles[2]);
  } else if (i == 3) {
    tiles[3] ++;
  } else if (i == 4) {
    tiles[4] ++;
  } else if (i == 5) {
    tiles[5] ++;
  } else if (i == 6) {
    tiles[6] ++;
  }

}

Upvotes: 1

Views: 1386

Answers (1)

Ilona Hari
Ilona Hari

Reputation: 533

When are you checking the values? How many objects on the scene have this script attached to it? It seems to me that you have the script attached to multiple objects and Random() is executed multiple times. Ex.

  • Obj1 --> Start() --> populate static array
  • Obj2 --> Start() --> re-populate static array
  • Obj3 --> Start() --> re-populate static array

In the end, all objects will have the same static array, generated by Obj3

Upvotes: 1

Related Questions