Skip to content

Instantly share code, notes, and snippets.

@saint-angels
Created March 25, 2016 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save saint-angels/a8bd4e336cdff3a8604a to your computer and use it in GitHub Desktop.
Save saint-angels/a8bd4e336cdff3a8604a to your computer and use it in GitHub Desktop.
You probably have a bunch of components that you want to keep a constantly updated list of too. Maybe all your enemies, or maybe all your bullets. (For Unity3D engine, from http://garry.tv/2015/06/14/unity-tips/)
using UnityEngine;
using System.Collections.Generic;
public abstract class ListComponent<T> : MonoBehaviour where T : MonoBehaviour
{
public static List<T> InstanceList = new List<T>();
protected virtual void OnEnable()
{
InstanceList.Add( this as T );
}
protected virtual void OnDisable()
{
InstanceList.Remove( this as T );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment