Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by babji3 · Jun 18, 2015 at 06:24 AM · c#scripting problemgameobjectpositiongrid

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image babji3 · Jun 18, 2015 at 08:41 AM 0
Share

i have to take object x and y position?

avatar image Eno-Khaon · Jun 18, 2015 at 09:03 AM 0
Share

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.

avatar image babji3 · Jun 18, 2015 at 09:19 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image babji3 · Jun 18, 2015 at 09:55 AM 0
Share

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??

avatar image Eno-Khaon · Jun 18, 2015 at 10:08 AM 0
Share

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.

avatar image babji3 · Jun 18, 2015 at 10:15 AM 0
Share

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?

avatar image Eno-Khaon · Jun 18, 2015 at 10:29 AM 0
Share

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;
avatar image babji3 · Jun 18, 2015 at 10:47 AM 0
Share

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..

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges