Having some trouble...

I’m new to Unity, and programming.
I am hoping to make a chess game.
I understand that it is quite a project for a first game.

I have a chess board 64 flat cubes, as child to previously empty GameObject now named “Board”.
Each flat cube is named A 1 - H 8 respectively, and is part of an object array with 65 elements with the first
element empty. so A 1 is at element 1.
Each chess piece is the child of its own starting flat cube.

My plan was to:

  • Use the parent/child relationship to change locations on the grid/array
  • For AI it was going to be simple. run calculations on each cube, can an enemy piece move onto this cube, cube gets a score of -10 per enemy, and +10 for each ally that can enter that cube. among other qualifiers as well and move to the cube with the highest score.
  • Write a script for legality of moves per piece.

My trouble is that I can not figure out for the life of me,(after 1 week of searching thinking and rethinking)
How to write a script that will check the array to see if there is a piece on it in order to write a script for legal moves.

I’m not asking for a complete code. I just would like a nudge in the correct direction with which keywords I need to look into, or even explaining the kinds of steps the code needs to go through.

You’re having problems determining how to retrieve the child of an object and the parent of an object, right? You can access these through the Transform object attached to each GameObject. The only complication is that the GetChild function requires an index with the first child being zero.

http://docs.unity3d.com/ScriptReference/Transform-parent.html

GameObject parentObj = childObj.transform.parent.gameObject;

http://docs.unity3d.com/ScriptReference/Transform.GetChild.html

GameObject childObj = parentObj.transform.GetChild(index).gameObject;

For a board game like this, you probably want to use an actual array to keep track of what’s where — don’t just rely on inspecting the scene hierarchy.

This means that whenever a piece moves, you need to update your array and also update the objects in the scene.

@Ryiah Thanks for the info and links. I’m trying to get them working now. I still feel like I’m missing some bit of understanding, but hey time for some more research and trials.

@JoeStrout Thanks for pointing out that I will need to update an array… I’ll have to look into that, but what do you mean by an ‘actual array’ is it different from the array that I made?

public GameObject[ ] grid = new GameObject[65];

then in unity moving the cubes A 1 - H 8 into the 65 elements of that array.

That’s exactly what I meant. I didn’t realize you were already doing that — I thought you were trying to get all your information about game state out of the transform hierarchy.

But in that case, I don’t understand your question:

If you have and maintain this array, then you simply look at grid[location] to see if there is a piece there, no?

1 Like

@JoeStrout
That is what I am trying to accomplish. I know that I can call information from the array and from there find out what the child piece is. It is the how that has been eluding me. I couldn’t spend much time on it last night after Ryiah linked that info to me. So that is what I will be playing around with trying to get a good handle on after the events/festivities of the day.

Just so I’m clear on how to look at that info from the array. I would-
{
ArrayOfTiles arrayOfTiles;
GameObject gamePiece;
void Start()
{
arrayOfTiles = GetComponent();
gamePiece = arrayOfTiles[1] //get child[0] if the gamepiece I am looking for is first sibling. which I still need to look up.
print(gamePiece);
}
}

No… I don’t know exactly what ArrayOfTiles is, except that you are getting it with GetComponent, so I presume it’s a component (probably a MonoBehavior). So, most likely, it doesn’t have an indexing operator, so you can’t say arrayOfTiles[1].

Earlier you said you had a proper array (i.e. not a MonoBehaviour, but an array of them):

public GameObject[] grid = new GameObject[65];

So, this you would access as grid[1], grid[0], grid[row*8+column], whatever.

If the problem is that you have this grid property in a different script from where you’re trying to access it, well, that’s a different problem. But as you’re new to all this, I might advise you in this case to try and keep it all in one script, for simplicity’s sake.

@JoeStrout
You are correct, ArrayOfTiles is a script on another object that has the array in it.

Your explanations are much appreciated. I’ll try and put as much of it as I can in one script