• 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 ceruleanmacaw · Dec 09, 2019 at 04:38 PM · movementgridgridmove

Tell an object that a position in grid is occupied

I have built a grid with some nulls(called nulos here) to keep track of positions and if they are occupied by another object:

 private void Init()
     {
         matriz = new int[3, 3, 3];
         nulos = new GameObject[27];
         int n = 0;
         for (int i = 0; i < nulos.Length; i++)
             nulos[i] = Instantiate(nulo);
 
         for (int i = 0; i < 3; i++)
             for (int j = 0; j < 3; j++)
                 for (int k = 0; k < 3; k++)
                 {
                     nulos[n].transform.position = new Vector3(nulos[n].transform.position.x + i, nulos[n].transform.position.y + j, nulos[n].transform.position.z + k);
                     Cubo = nulos[n].GetComponent<CubeProperties>();
                     nulos[n].name = i.ToString() + j.ToString() + k.ToString();
                     n++;
 
                 }
     }

"Cubo" contains a script called CubeProperties that has a bool "free" that I'm using to check if the position a block is trying to move to its already in use.

In each object/block in my grid I have a script called "movingCube" that has a method moveIt called in update:

 void Start()
     {
         GameObject.Find(x.ToString() + y.ToString() + z.ToString()).GetComponent<CubeProperties>().free = false;
     }
 
      public void moveIt()
             {
     
                 if (Input.GetKeyDown(KeyCode.W))
                 {
                     if (GameObject.Find((x + 1).ToString() + y.ToString() + z.ToString()) != null)
                     {
                         if (GameObject.Find((x + 1).ToString() + y.ToString() + z.ToString()).GetComponent<CubeProperties>().free == true)
                         {
                             transform.position = GameObject.Find((x + 1).ToString() + y.ToString() + z.ToString()).GetComponent<CubeProperties>().transform.position;
                             GameObject.Find(x.ToString() + y.ToString() + z.ToString()).GetComponent<CubeProperties>().free = true;
                             GameObject.Find((x + 1).ToString() + y.ToString() + z.ToString()).GetComponent<CubeProperties>().free = false;
                             x++;
                         }
                     }
         
                 }
 

The problem is: I though that by asking

if (GameObject.Find((x + 1).ToString() + y.ToString() + z.ToString()).GetComponent().free == true)

it wouldit would let me move only if the space was not already occupied by another object, but right now it does nothing as it allows me to move without recognizing the other objects and overlapping them.

At start each cube tells the grid their initial position, so I don't see why it is not working? Any insight of what I've done wrong?

Comment
Add comment · Show 2
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 Dougtay · Dec 10, 2019 at 06:09 PM 0
Share

how and where are you assigning the .free value? I don't see that in the first code block.

avatar image Larry-Dietz · Dec 10, 2019 at 06:24 PM 0
Share

It's a convoluted process that you are implementing, but on the surface, I don't see why it would not work, assuming that the x, y and z variables are being set to your transform positions x,y and z values before moveIt is called. Have you confirmed in the inspector at runtime, that the Free boolean is being set as expected?

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

177 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Faults with Grid-Based movement... 2 Answers

How to let player move for specific number of times in gird base system.,Implement a move system in grid where player can move for specific number then dies if he did not meet game end/ 0 Answers

Having trouble with grid movement 1 Answer

how to make objects move in rows and columns? 0 Answers

2D Game, Player Not to move if a wall is next to it C# 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges