I have made floor where at each pixel I have instantiated a cube. I have 400 cubes. Now I have created an empty game object, I have attached a script now what I want to do is …
if(Input.GetButtonDown("Fire1")){
//how do I get position of a game object available in scene?
}
I mean If I click a cube I should get its center position as a vector3.
You need to cast a ray from the screen to the mouse position:
float rayLength = 100.0f; // This is the length of the ray. You need to figure out how long it must be in order to hit all of the cubes in the scene.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, rayLength))
{
if (hit.collider != null)
{
Debug.Log(hit.collider.transform.position);
}
}
In order for the ray to hit the game object in the scene, the game objects must have a Collider component.