Hi there, Im a designer working on a prototype its a kind of triple match game Im very new to Coding basically I self taught myself everything.
Here I struck with one thing which I cundt take it further I would be really appreciate all kind of ur inputs to overcome this issue…So this is the thing the In the below code I want three same identical objects should be visually destroy from the horizontal grid once they formed and should allow other objects to form on the grid and but unfortunately the three identical objects not destroying from the grid.
Thanks in advance…!! (Note: For ur reference triplematch 3d game mechanic )
public class GridManager : MonoBehaviour
{
public GameObject[] gridSlots; // Array of references to the seven grid objects
public float timeLimit = 10.0f; // Time limit for the level in seconds
private int score;
private float remainingTime;
private bool[] gridFilled;
private ObjectLogic[] placedObjects; // Array to track objects placed in the grid
void Start()
{
placedObjects = new ObjectLogic[gridSlots.Length];
gridFilled = new bool[gridSlots.Length];
remainingTime = timeLimit;
}
void Update()
{
remainingTime -= Time.deltaTime;
if (remainingTime <= 0.01f)
{
// Handle time limit reached (e.g., display message, game over)
}
}
public bool PlaceObject(ObjectLogic objectLogic)
{
for (int i = 0; i < gridSlots.Length; i++)
{
if (objectLogic != null && !gridFilled[i])
{
if (gridSlots[i].transform.childCount > 0)
{
Destroy(gridSlots[i].transform.GetChild(0).gameObject);
}
// Instantiate the prefab directly and store a reference to it
GameObject newObject = Instantiate(objectLogic.objectPrefab, gridSlots[i].transform.position, Quaternion.identity);
// Set the parent of the instantiated object to the grid slot
newObject.transform.SetParent(gridSlots[i].transform);
// Ensure that the ObjectLogic component is attached to the instantiated object
ObjectLogic newObjectLogic = newObject.GetComponent<ObjectLogic>();
if (newObjectLogic == null)
{
Debug.LogError("ObjectLogic component is missing from the instantiated object.");
return false;
}
// Assign the ObjectLogic component to the placedObjects array
placedObjects[i] = newObjectLogic;
gridFilled[i] = true;
return true;
}
}
return false; // No empty slots available
}
// Function to check for matches, update score, and handle clearing (optional)
public void CheckForMatches()
{
int matchCount = 0; // Variable to track matches found
// Check for horizontal matches
for (int i = 0; i < 3; i++)
{
if (placedObjects[i] != null && placedObjects[i].objectType == placedObjects[i + 1].objectType &&
placedObjects[i + 1].objectType == placedObjects[i + 2].objectType)
{
// Match found!
HandleMatch(i, i + 1, i + 2);
matchCount++;
}
}
if (matchCount > 0)
{
// Populate empty slots with new objects (optional)
PopulateEmptySlots();
}
}
private void HandleMatch(int index1, int index2, int index3)
{
// Update score (implementation depends on your scoring system)
UpdateScore(10); // Example: Add 10 points per match
// Remove matched objects from the grid and array
for (int i = index1; i <= index3; i++)
{
if (gridSlots[i].transform.childCount > 0)
{
Destroy(gridSlots[i].transform.GetChild(0).gameObject);
}
placedObjects[i] = null;
gridFilled[i] = false;
}
// Activate visual/sound effects for match completion (optional)
}
private void PopulateEmptySlots()
{
// Optional: Repopulate empty slots with new objects
for (int i = 0; i < placedObjects.Length; i++)
{
if (placedObjects[i] == null)
{
// Instantiate a new object in a random available slot
PlaceRandomObject();
break; // Place only one object per call to prevent endless loops
}
}
}
private void PlaceRandomObject()
{
// Find an empty slot
int emptySlotIndex = -1;
// If an empty slot is found, place a random object
if (emptySlotIndex != -1)
{
int totalObjectTypes = 6;
string randomPrefabName = "randomPrefabName" + Random.Range(1, totalObjectTypes + 1) + "Prefab";
GameObject newObject = (GameObject)Resources.Load(System.IO.Path.Combine("Prefabs", randomPrefabName));
newObject = Instantiate(newObject, gridSlots[emptySlotIndex].transform.position, Quaternion.identity); // Instantiate directly
placedObjects[emptySlotIndex] = newObject.GetComponent<ObjectLogic>();
// Check if the object was successfully instantiated (optional)
if (placedObjects[emptySlotIndex] != null)
{
// Access components or properties of the new object here
}
}
}
private void UpdateScore(int points)
{
// Update your score variable based on your game logic
score += points;
// Update UI element displaying the score (assuming you have one)
ScoreUI.text = score.ToString();
}
}
Are you sure you're not getting errors in the console? Because in your CheckForMatches() function, you're not checking if (placedObjects [i + 1] != null), and if (placedObjects [i + 2] != null). You're just assuming they won't be null.
– ArachnidAnimal