- Home /
Need to select whatever object is at a certain position
I am new to Unity and thought I would make a simple chess game to learn the engine but alas I have already hit a snag. I created my board using 64 (8x8) cubes. As of right now I want to be able to change the color of a cube to show a move is valid on that square. In otherwards if you pick up a pawn the first square infront should turn green. I have the cubes in an easy grid system so i know the location of the cube I need but the ID of that cube as it will change depending what square im on. Is there a way to select an object with its transform info. In pseudocode it would look like...
Var canMoveHere : GameObject = findObjectAtPosition(0,2,0);
Answer by whydoidoit · Dec 28, 2012 at 08:52 AM
You'd probably be better off having a 2D array of transforms (or a 1D array where every 8th element is a new line) and then you can just look up the transform, get its renderer and fiddle with it:
var squares : Transform[]; //You would drag and drop the squares onto here using the inspector
function GetSquare(x : int, y : int) : Transform
{
return squares[x + y * 8];
}
function SetTheColorOfASquare(x : int, y : int, color : Color)
{
GetSquare(x,y).renderer.material.color = color;
}
Answer by Pysassin · Dec 28, 2012 at 08:48 AM
I just tagged all the tiles then searched for all the objects by tag and compared the positions in a for loop. Coloring the cube is remaining dificult. Cant get it to change back to the original color.
Edit: ultimately went with an overlsy but I like the idea mike and will keep it in mind.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Replace variable with new game object after it is made null - not working 0 Answers
Using script's method from all of the gameobjects that has that script 2 Answers
How to create 3D game object in specified pixel size? 2 Answers
Unity3D Pressure Plate request. 3 Answers