Dictionary trygetvalue not returning expected answers

Hey guys this might be fairly long winded so please bare with me. I have a grid of tiles being produced and stored in a dictionary, then in a different class I am searching through said board in order to set positions to spawn game objects.

Vector2 gridSize = calcGridSize();
		//Game object which is the parent of all the hex tiles
		GameObject hexGrid = new GameObject("HexGrid");
		
		//board is used to store tile locations
		board = new Dictionary<Point, Tile>();
		
		//set hexgrid to the invisible hexgrid layer
		hexGrid.layer = 8;
		
		for(int y = 0; y < gridSize.y; y++)
		{
			for(int x = 0; x < gridSize.x; x++)
			{
				//GameObject assigned to Hex public variable is cloned
				GameObject hex = (GameObject)Instantiate(Hex);
				
				//Current position in grid
				Point gridPos = new Point(x,y);
				
				hex.transform.position = calcWorldCoord(gridPos);
				hex.transform.parent = hexGrid.transform;
				
				//TileBehaviour object is retrieved
				var tb = (TileBehaviour)hex.GetComponent ("TileBehaviour");
				
				// y/s is subtraced from x because we are using straight axis co-ord system
				tb.tile = new Tile((int)x - (int)(y / 2), (int)y);
				
				board.Add(tb.tile.Location, tb.tile);
				
				//set hex to the invisible hex layer
				hex.layer = 8;
			}
		}

FYI the gridsize is set in the inspector, for the sake of visualization let’s say this particular one is 50 x 50.

in a different class I have a method setting up a board variable and then calling this method:

int index =0;
		for(int planets = 0;planets < 100;planets++)
		{
			Point randomPoint = new Point(Random.Range(0,50),Random.Range(0,50));
			
			
			if(Board.TryGetValue(randomPoint, out tile)){
				if(tile.canSpawnPlanetHere)
				{
					GameObject star = (GameObject)Instantiate(Resources.Load("Prefabs/Stars/Black Hole System"));
					star.transform.parent = transform;
					
					star.BroadcastMessage("setPlanetToSpawn", StarBehaviour.PlanetToSpawn.Primary);
					
					StarBehaviour starScript = star.GetComponent<StarBehaviour>();
					starScript.setPosition(randomPoint);
					
					star.BroadcastMessage("spawnPlanet");
					index++;
				
				}
			}
			else{Debug.Log ("this tile dont exist" + randomPoint.X + "," + randomPoint.Y);}
			Debug.Log(Board.Count);
		}
		
		Debug.Log(index + " planets spawned");

So, the dictionary board should contain 2500 tiles, with key’s being points from (0,0) to (50,50). And the code should spawn 100 planets randomly over the grid.

The problem is that it only successfully produces so many, generally between 60-75 ish. The rest of them tell me from the debug.log that the desired tile doesn’t exist. Normally I would expect that I had made an error with indexing and so expect the erroneous positions to be at the limits, for instance (50,50) might not exist it should be (49,49). However the points in question are seemingly completely random. Points such as (38,47) are returning false in trygetvalue. I ran the board.count through debug.log just to make sure there was indeed 2500 tiles still in there.

I am truly stumped, I can’t figure out what’s going on here, any one have any idea?

  • Edit - worth noting that as i lower the max limit on the random point, the success rate goes up. Having it randomly produce between (0,0) and (35,35) led to 3 100% spawns and 1 98% over 4 attempts.

I found the solution to this problem, as it turns out it would have been very difficult for some one to guess the reason. I figured it must be outside influence not included in the above code. For any one interested it was because I was using a straight co-ordinate system for the board, but a squiggly axis co-ordinate system for the spawning. Fiddling around with the x and y values produced exactly the effect required =).