- Home /
Trying to figure out tags,
So I looked up a way to show a numerical value on how many of a certain gameObject there is currently in my scene and I came across Tags. I am trying to build a Block Breaker game. What I am basically thinking of doing is creating a tag called "Bricks" and then tagging all the brick objects as "Bricks". What I want to do is have a test run so that when there are no longer any bricks in my scene that it loads the next one. To test that my logic works I made it print the number of bricks each frame to show once I break a brick the number will go down. My issue is after I break the last brick it still shows there is 1 brick. Can you guys help out? Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Brick : MonoBehaviour {
public int maxHits;
private LevelManager levelManager;
private int timesHit;
private int brickObject;
// Use this for initialization
void Start () {
timesHit = 0;
levelManager = GameObject.FindObjectOfType<LevelManager>();
}
// Update is called once per frame
void Update () {
brickObject = GameObject.FindGameObjectsWithTag ("Bricks").Length;
print (brickObject + " objects left");
}
void OnCollisionEnter2D (Collision2D col)
{
timesHit++;
if (timesHit >= maxHits) {
Destroy (gameObject);
}
}
// TODO Remove this method once we can actually win
void SimulateWin ()
{
levelManager.LoadNextLevel();
}
}
The end result I am looking for is once I play the game it will constantly tell me in the console how many bricks are left in my scene and when there are none left it should say "0 objects left" but instead it still shows "1 object left" after the last object is destroyed. Testing this way so that once it can show there are no more objects I can load the next scene.
Answer by Hellium · Feb 19, 2018 at 05:59 PM
Relying on tags is a very bad idea in your case IMHO.
You should have a GameManager or BricksManager responsible for handling the game. This task hould not be handled by the individual bricks. You should use events (or actions in the following example) to warn your gamemanager a given brick has been destroyed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Brick : MonoBehaviour {
public int maxHits;
private int timesHit;
private int brickObject;
public System.Action<Brick> OnBrickDestroyed; // Used to warn the gameManager that the brick has been destroyed
// Use this for initialization
void Start ()
{
timesHit = 0;
}
void OnCollisionEnter2D (Collision2D col)
{
timesHit++;
if (timesHit >= maxHits)
{
// Warn the gameManager
if( OnBrickDestroyed != null)
OnBrickDestroyed.Invoke( this );
Destroy (gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
// Drag & drop all your bricks in the inspector
[SerializeField]
private List<Brick> bricks;
// Drag & drop the LevelManager
[SerializeField]
private LevelManager levelManager;
// Use this for initialization
void Start ()
{
if( levelManager == null )
levelManager = GameObject.FindObjectOfType<LevelManager>();
if( bricks == null || (bricks != null && bricks.Count == 0) )
bricks = new List<Brick>( GameObject.FindObjectsOfType<Brick>() ) ;
for( int i = 0 ; i < bricks.Count ; ++i )
{
bricks[i].OnBrickDestroyed = OnBrickDestroyed ;
}
}
void OnBrickDestroyed( Brick destroyedBrick )
{
bricks.Remove( destroyedBrick );
if( bricks.Count == 0 )
{
SimulateWin() ;
}
Debug.Log( "Remaining bricks: " + bricks.Count );
}
void SimulateWin ()
{
levelManager.LoadNextLevel();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612152942im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
All tags disappear 1 Answer
LoadScene do not work well 2 Answers
Help with a Menu-Script 1 Answer
Did any one got and issue with finding tag in 5.5.1 or later 0 Answers