How to check neighboring GameObjects?

Hello all,

I have two arrays working together. One is a multidimensional array with ints (here I can place a number which represents a GameObject). The other is an array with the actual GameObjects. I made Element 0 of it an Empty GameObject. Now I want if there is an Empty GameObject beneath another GameObject, this GameObject and the Empty one switch places so it looks like the game object is falling 1 position.

I guess I’ll have to check for Empty GameObjects and if there is another GameObject above it. So where should I place the script for this? On the board (the place with all the GameObjects) or on all the GameObjects itself or only on the Empty GameObject?

And how could I check the neighboring GameObjects?

Thank you for your time.

neighbouring in the scene, or neighbouring in the array? If it’s in the array, they’re just at an index 1 away (depending the dimension).

Also, what is up with the 2 arrays, one for ints, and 1 for the gameobjects. What are the ints for? That sounds like a very strange setup.

What is it you’re exactly attempting to do? Is this like a chess game or something?


It’s two arrays so I have an easy visual way to change the board. It will be a puzzle game, a combination of a matching game and a rubix cube (sort of). I think this will be a good way to create a level.

I mean neighboring in the (Board) array.

It exists merely to allow for a pretty inspector?

If that is so… then write it as an editor script for only inspector view. But runtime code just needs to deal with the one array of gameobjects. Otherwise you’re going to complicate the heck out of this things trying to maintain both arrays and keeping them synced.

void Start()
    {
        for(int y = 0; y < 9; y++)
        {
            for(int x = 0; x < 9; x++)
            {
                int index = myboard.rows[y].row[x];
                if(index >=0 && index <cubes.Length)
                {
                    GameObject obj = cubes[index];
                    Vector3 pos =new Vector3(x, -y, 0);
                    Instantiate(obj, pos, Quaternion.identity);
                }
            }
        }
    }

This is the code on it.
I think I don’t have to worry about the Cubes array because it only holds the GameObjects. The only place where the action is, is in the Board array.
Do you think I should place the checking for neighbors in the same script as above or should I make a script for the Cubes and do the checking there?

OK, whatever, so this is just weird. But lets move forward with it, you have your method of working, we’ll stick with that if it works for you.

SO…

What do you mean it looks like it’s falling? You can swap the position in the array, but that doesn’t mean it’s going to move on screen. The array doesn’t control its position. Unless you’re constantly updating the screen based on its position in the array.

In that case, yeah… just swap its position in the array, then whatever is updating the position will move it.

That’s up to you… it’s your design. It’s weird enough as is, I couldn’t tell ya where to put it without more information about it.

But what if I would only make one array, would that make everything easier?

Honestly you still haven’t really answered any of my questions because we got on the tangent about your weird design.

But anyways, I asked.

I honestly have no idea what you’re attempting to do. You have an array of ints, and an array of cubes… what is this supposed to represent? What do you mean by neighbour? Why do you need the neighbour?

I’m pretty sure the array of ints represents indexes into the array of GameObjects. Not that odd of a design, it makes it easy to switch which prefab is used for a given element type.

On the original question - I think what you’d want to do is to have a 2D array of GameObjects (as a private field) and store the GameObjects into it as you create them. Then you can easily check what object (if any) is in a given cell.