How to check if there is a gameobject on a specific gameobject location?
I'm a beginner on unity I'm trying to make a checker game on unity so i already have a board which is made of gameobject cube 2d array and pieces which is made of a list of gameobject and the movement of the pieces (diagonally) is already ok.
So my problem is my piece can move over a occupied tile and it will overlap and also my piece still can't capture other piece. Please try help me thank you!
Answer by Glurth · Feb 24, 2016 at 09:06 PM
I'll assume that each piece has an x,y values that define it's position on the board. In this case you will want to loop through your list of piece and check each one for the x,y coordinate you want to check. e.g. (without your code, I'm just making up variable names for the following code fragment. This has not been compiled, example only)
foreach(piece in pieceList)
{
if(piece.x == checkX)
if(piece.y == checkY)
foundPiece=piece; //do somthing with the piece you found
}
Well that exactly what i'm just doing right know thank you for letting me know i'm on the right track :)
It might be some unexpected situation that is messing you up. For example: with the loop I provided, should more than ONE piece occupies the checked square, whichever one happens to be later in the list will be the "found piece". That might not be what you want, in which case you wouldl need to add more conditionals inside the loop to account for that.