Fan4i
Fan4i

Reputation: 115

How to get point position in the grid by using dictionary?

can someone help me? I made a grid for my board. When I'm trying to get Point position of the square in the board, console returns only (0,0).

This is my point code:

public struct Point 
{
    public int X {get; set;}
    public int Y {get; set;}

    public Point(int x, int y){
        this.X = x;
        this.Y = y;

    }


}   

This is script where every square get a point in the grid when instantiated:

public Point GridPosition { get; set; }


public void Setup(Point gridPos, Vector3 worldPos)
{
    this.GridPosition = gridPos;
    transform.position = worldPos;
}

private void OnMouseDown(){

    Debug.Log (GridPosition.X + ", "+ GridPosition.Y );

}    

And this is my main script with Dictionary part:

public static Dictionary<Point,Grid> Tiles { get; set; }

void Start()
{
    CreateLevel ();
}

void CreateLevel()
{

    Tiles = new Dictionary<Point,Grid> ();
}

private void PlaceTilesColliders(Vector3 tileStart, float tileOffset){

    for (int y = 0; y < 8; y++) 
    {
        for (int x = 0; x < 8; x++) 
        {

            TileCollider.GetComponent<Grid> ().Setup (new Point (x, y), new Vector3 (tileStart.x + (tileOffset * x), tileStart.y - (tileOffset * y), 0));

            Tiles.Add (new Point (x, y), Instantiate(TileCollider).GetComponent<Grid>());


        }
    }  
}     

So, console return every time (0,0), don't matter which square was clicked. Can someone explain me how to get true point position of the square in the grid?

Upvotes: 1

Views: 488

Answers (2)

avariant is essentially correct with their answer, but I'd like to point out what your code is actually doing and why you're getting the values you're getting.

Lets look at this loop:

for (int y = 0; y < 8; y++)  {
    for (int x = 0; x < 8; x++)  {
        TileCollider.GetComponent<Grid> ().Setup (new Point (x, y), new Vector3 (tileStart.x + (tileOffset * x), tileStart.y - (tileOffset * y), 0));
        Tiles.Add (new Point (x, y), Instantiate(TileCollider).GetComponent<Grid>());
    }
} 

We loop over Y and X (this is fine) and then we call TileCollider.GetComponent<Grid> (). Wait, hold on, TileCollider? What is this? This can't be one of our files in the scene, we haven't used our X and Y coordinates to go fetch a GameObject from the scene to get this reference...

That means anything we do to it has no effect on our tiles in the game world! And because the reference isn't updated, we continuously update it's values to the new X and Y positions, overwriting what we'd already done and having no effect on anything.

Oops.

And this is why avariant says that you need to call Instantiate and create a new tile, then get the component from that GameObject and call Setup() on it.

Upvotes: 0

avariant
avariant

Reputation: 2300

Try Instantiate first, then configure the resulting new Grid and add to the dictionary.

for (int y = 0; y < 8; y++) 
    {
        for (int x = 0; x < 8; x++) 
        {

            GameObject newGrid = Instantiate(TileCollider);
            newGrid.GetComponent<Grid>().Setup(new Point (x, y), new Vector3 (tileStart.x + (tileOffset * x), tileStart.y - (tileOffset * y), 0));

            Tiles.Add(new Point (x, y), newGrid.GetComponent<Grid>());
        }
    } 

I would recommend, though, that you pay attention to parenting, as right now the instantiated objects have no parent.

Upvotes: 1

Related Questions