Ignition / ObjectCounter

Namespace Playniax.Ignition

Inherits from MonoBehaviour

Script can be found in Assets/Playniax/Framework/Ignition/Framework/Core/Scripts (MonoBehaviour)/ObjectCounter.cs

Class ObjectCounter

Description

The ObjectCounter keeps track of the number of GameObjects enabled in the scene.

In order for this to work, you need to add the ObjectCounter component to the GameObjects that need to be included in the count.

Example can be found in Assets/Playniax/Framework/Ignition/Scenes/01 - Framework/ObjectCounter/Scenes/ObjectCounter.unity

Example script can be found in Assets/Playniax/Framework/Ignition/Scenes/01 - Framework/ObjectCounter/Scripts (MonoBehaviour)/ObjectCounter_Example_Spawner.cs
using UnityEngine; using UnityEngine.UI; public class ObjectCounter_Example_Spawner : MonoBehaviour { public GameObject prefab; public float interval = .2f; private float timer = 0.0f; public Text text; void Awake() { if (prefab && prefab.scene.rootCount > 0) prefab.SetActive(false); } void Update() { // Spawn the objects. timer += Time.deltaTime; if (timer >= interval) { timer = 0; var instance = Instantiate(prefab); instance.SetActive(true); } } }
Example script can be found in Assets/Playniax/Framework/Ignition/Scenes/01 - Framework/ObjectCounter/Scripts (MonoBehaviour)/ObjectCounter_Example_Bullet.cs
using UnityEngine; using Playniax.Ignition; public class ObjectCounter_Example_Bullet : MonoBehaviour { void Start() { _renderer = GetComponent<Renderer>(); _direction = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), 0).normalized; } void Update() { transform.localPosition += _direction * 10 * Time.deltaTime; if (CameraHelpers.IsVisible(_renderer) == false) gameObject.SetActive(false); } Vector3 _direction; Renderer _renderer; }
Example script can be found in Assets/Playniax/Framework/Ignition/Scenes/01 - Framework/ObjectCounter/Scripts (MonoBehaviour)/ObjectCounter_Example_UI.cs
using UnityEngine; using UnityEngine.UI; using Playniax.Ignition; public class ObjectCounter_Example_UI : MonoBehaviour { public Text text; void Awake() { if (text == null) text = GetComponent<Text>(); } void Update() { // Get and display the number of objects. text.text = "The ObjectCounter counts " + ObjectCounter.count.ToString() + " objects."; } }