Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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, assu$$anonymous$$g 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.

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

Having trouble with grid movement 1 Answer

how to make objects move in rows and columns? 0 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

Faults with Grid-Based movement... 2 Answers

How to detect collision of a wall with grid movement? 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