- Home /
How to debug the object position in the grid of objects?
Hi All, I created 5x5 grid of objects in unity c#. My questions is ,when I click on certain object, I want debug(print) that object position.
For Example, i click on object Grid[3,3] and i want debug(print) that object grid position. Example "Selected : [3,3]". Please help me,thanks
The problem here is that there's a lot of missing information that we can't offer any meaningful assistance.
Are your grid objects meshes (i.e. planes, cubes, spheres, etc.)? Are they managed by a single script on another object? Do they each have their own script dictating their position?
Are your grid objects GUI elements (Rect(s), for example)?
The type of objects your grid is made from dictates the means by which detection can be handled. Without information on how you're assembling your grid, there's no way to offer you assistance without simply speculating regarding what you may or may not be doing.
Sorry for that.. $$anonymous$$y Grid Objects is a set of Cubes.(i.e Prefab).. I attached below script to an Empty Gameobject..
public int Width;
public int Height;
public GameObject Cube;
public GameObject[,] grid ;
void Start ()
{
grid = new GameObject[Width, Height];
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
{
GameObject go =GameObject.Instantiate(Cube) as
GameObject;
Vector3 position = new Vector3(x, y, 0);
go.transform.position = position;
grid[x, y] = go;
GameObject GridGB = GameObject.Find ("Empty_GRP");
go.transform.parent = GridGB.transform;
}
}
What i want is, If i click on certain object ,i want Debug(Print) that object position. Example If i select Grid[3,3] object i want print in the console like this "Selected: 3,3".. thanks
Answer by Eno-Khaon · Jun 18, 2015 at 09:39 AM
Thank you kindly for the updated information. One way to approach this would be to use a Raycast and check whether what it hits is one of the instantiated cubes.
void Update()
{
if(Input.GetMouseButtonDown(0)) // Left Click
{
// Data to be read out from the Raycast
RaycastHit hit;
// The ray to fire is from the camera to the mouse position
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// Then, try the Raycast. If it hits something, the if statement is true
if(Physics.Raycast(ray, out hit, 100))
(
// Inside, check your grid for a GameObject matching the collider of the GameObject hit.
// Note that each cube will need a collider for this method to work, however.
for(int x = 0; x < Width; x++)
{
for(int y = 0; y < Height; y++)
{
if(hit.gameObject == grid[x, y])
Debug.Log("Selected: " + x + "," + y); // Or (x + 1) and (y + 1), as desired
}
}
}
}
}
With this added to your script which generated the cubes, you'll be able to click on an object and find out which spot it was at.
Thanks for your help it works..
I Want delete the middle object, i.e grid[3,3], I do like this..
Destroy (grid [3,3]);
is this correct? ,if correct how do i null that grid position ? I dont have the object in that position ,so i have to null the object .so how i can do that??
First off, the grid would go from 0-4 in each dimension in a 5x5 pattern, so [2, 2] would be the actual center point.
That said, if you don't put an object there in the first place, then it would already be null.
If you do instantiate a GameObject in the center point, but want to get rid of it, then your example does work. You'd destroy the GameObject referenced at that grid location, leaving that position in the grid as null.
Just be sure to specify the matrix position, however, because using
Destroy(grid);
ins$$anonymous$$d would destroy the entire grid.
you mean if i destroy [2,2] object , the grid is null on that position? or i have write another like below destroy line?like this??
Grid [2,2] = null;
How to update the grid if the object is not their?
that's also an acceptable way of getting that result. It's really more of a safety measure, though, because once the GameObject is destroyed, the location in memory where it existed will no longer recognize it as a GameObject (starting on the next frame). Therefore, it will be null ins$$anonymous$$d.
So you can either:
A) Not create a cube if the intended slot is the center slot
if(x != 2 && y != 2)
Instantiate(Cube);
B) Destroy the cube in the center slot
Destroy(grid[2,2]);
C) Set the center slot to null (GameObject still exists in scene)
grid[2,2] = null;
Thanks for your helpful suggestions . Actually am working on peg solitaire game, i have one issue when i move the peg above the another ,i want delete the middle peg, The selection ,movement is working great ,but i don't know how do i delete the middle peg.. here is my code and image ,thanks plz help me ,am working this on a while..
Your answer
Follow this Question
Related Questions
Update character's position after animation 1 Answer
Resize gameObject when it has extended a restricted area 0 Answers
How to make the Health bar on Enemys head not be in relation to Player(main) Camera? 2 Answers
Multiple Cars not working 1 Answer
How do i change a sprite when another gameobject with the same prefab is colliding / is near 1 Answer