I’m making a simple match three game, I have created a board with tiles, a function to get random pieces, a function to place a random piece on the board, and a function to fill all the board with. But when I called the PlaceGamePiece() on FillRandom() it did not work. I assume that the PlaceGamePiece() needs 3 parameters and I tried to fill it but somehow did not work.
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PieceManager : MonoBehaviour
{
public GameObject[] gamePiecePrefabs; //an array of all the game pieces in the game as GameObjects.
private GamePiece[,] allGamePieces; //a 2-dimensional array holding all the current game piece's GamePiece scripts.
public Board board; //a reference to the Board class.
void Start()
{
board = GameObject.Find("Board").GetComponent<Board>(); //store the Board class.
allGamePieces = new GamePiece[board.width, board.height]; //constructs a new array of size width by height.
FillRandom(); //fill the board.
}
//Get a random game pieces from the array.
//Called in FillRandom().
GameObject GetRandomGamePiece()
{
//get a random number between 0 and all the possible game pieces - 1 in the gamePiecePrefabs array
//the .Length property of an array is not inclusive of the final number.
int randomIdx = Random.Range(0, gamePiecePrefabs.Length);
//safety check to make sure the array is populated in the Inspector panel.
if(gamePiecePrefabs[randomIdx] == null)
{
Debug.Log("WARNING: Element " + randomIdx + " in the GamePiecePrefabs array is reading as null");
}
//return the selected GamePiece to the function calling it.
return gamePiecePrefabs[randomIdx];
}
//places a game piece at the x and y passed in.
//Called in FillRandom().
public void PlaceGamePiece(GamePiece gamePiece, int x, int y)
{
//safety check to make sure the gamePiece passed in has a value.
if(gamePiece == null)
{
Debug.LogWarning("PIECEMANAGER: Invalid GamePiece!");
return; //break out of the method so the next lines don't run.
}
//move the gamePiece passed into the brackets by the function call to the x and y passed in
gamePiece.transform.position = new Vector3(x, y, 0);
//set the rotation back to zero in case we have accidentally rotated it.
gamePiece.transform.rotation = Quaternion.identity;
//call the SetCoord() method to populate GamePiece.xIndex and GamePiece.yIndex variables.
gamePiece.SetCoord(x, y);
}
//Fill the board with randomly game piece.
//Called in Start().
public void FillRandom()
{
for (int row = 0; row < board.width; row++)
{
for (int col = 0; col < board.height; col++)
{
//get random game pieces.
GameObject randomPiece = GetRandomGamePiece();
//instantiates game piece prefabs.
GameObject pieces = Instantiate(randomPiece, Vector3.zero, Quaternion.identity) as GameObject;
//parent pieces.
pieces.transform.parent = GameObject.Find("Pieces").transform;
//safety check if whether the random pieces is returned or not.
if(pieces == null)
{
Debug.Log("RANDOM PIECE not found!");
return;
}
else
{
PlaceGamePiece(); // HERE THE FUNCTION THAT HAVE ERROR.
}
//set game pieces to sorting layer "Pieces".
pieces.GetComponent<Renderer>().sortingLayerID = SortingLayer.NameToID("Pieces");
}
}
}
}
If you need the GamePiece() script:
Here it is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GamePiece : MonoBehaviour
{
public int xIndex; //the current x-coordinate of the game piece.
public int yIndex; //the current y-coordinate of the game piece.
//sets the x and y index to the arguments passed in
//Called by PlaceGamePiece()
public void SetCoord(int x, int y)
{
xIndex = x; //set xIndex to the x value passed in the function call.
yIndex = y; //set yIndex to the y value passed in the function call.
}
}
How to solve the problem, please help.