How to create a winning condition when a certain number of game objects have spawned

133 Views Asked by At

I am trying to get my 3-D Tic-Tac-Toe game project to work, I have game objects which are named cells that are instantitated I press OnMouseDown() click it makes a cell object spawn in its grid space. I don't want to use UI with the basic prefabs I created. Is there a way to get my game objects instantiated and once it reaches a certain number as a winning condition? I have considered using pathfinding but I am not certain if that would be the correct approach. I have looked every where to find a solution that is unique to my problem but could not find a solution. Perhaps, I am asking the wrong questions but I am desperate so that is why I came her to see if I could get input on how to approach this issue.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
public class Cell : MonoBehaviour
{  
    public GameObject cell;
    public GameObject negCell;
    public GameObject alter;
    
    public Transform transPos;

    [SerializeField]
    private bool isTapped = false;
   
    private int counted;
    public int gameObjectCount; 

    void Start()
    {
       gameObjectCount = GameObject.FindGameObjectsWithTag("Cell1").Length;

    }

    void Update()
    {

        
    }

    public void OnMouseDown(int counted) //click and point to create and deestroy objects 
    {
        counted = gameObjectCount;
        isTapped = true;
        transPos = alter.transform;       
        Instantiate(cell, transform.position, Quaternion.identity);
        StartCoroutine(WaitForObject());       
        Debug.Log("Alter Destroyed!");
        gameObjectCount++;
        DestroyGameObject();
        return;
    }


    IEnumerator WaitForObject()
    {
        if (isTapped == true)
        {
            Instantiate(negCell, -transform.position, Quaternion.identity);
            isTapped = false;
           
           
        }
        
        yield return new WaitForSeconds(3f);
        DestroyGameObject();
    }
    void DestroyGameObject()
    {   
        if(gameObject == alter)
        {
            DestroyImmediate(alter, true);
        }       
        else
        {
            DestroyImmediate(cell, true);
        }

       
    }
    
}

1

There are 1 best solutions below

0
On

There are two easy ways to achieve this.

The first one would be to add a static member in your class, let's say :

private static int _instanceCounter = 0;

This will act as a class instances counter.

All you have to do is to increment this variable every time you instantiate a new game object. Finally, base your win condition on the number of instances of the class you want.

You can also decrement this variable if for some reason at a moment you call the Destroy method on a specific game object.

The other way would be to use the FindObjectsOfType method from Unity which returns an array of all instances in your current scene. By accessing the length of this array, you'll have the number of instances.

However, this only count for the current number of instances when this method is called. Note that you can also include the inactive game objects from the scene (those which are in grey within your hierarchy panel).

You now have two ways to do it, depending on how you want to achieve your win condition, i.e. the total of game objects instantiated OR a specific number of game objects at a given time.