Skip to content

Instantly share code, notes, and snippets.

@nailuj05
nailuj05 / CollisionDetector.cs
Created February 26, 2022 23:24
A component that allows you to subscribe to the Collision Events on a object (even if the main script does not lie on the object with the collider)
using UnityEngine;
//A component that allows you to subscribe to the Collision Events on a object (even if the main script does not lie on the object with the collider)
public class CollisionDetector : MonoBehaviour
{
public delegate void Coll(Collider collider);
public event Coll CollisionEnter;
public event Coll CollisionStay;
public event Coll CollisionExit;
@nailuj05
nailuj05 / Singleton.cs
Created January 1, 2022 22:46
Clean Unity Singleton Implementation. Inherit from Singleton<ExampleClass> to use it. Access the Singleton using ExampleClass.main
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T instance;
public static T main
{
get
{
if (instance == null)
instance = FindObjectOfType<T>();
@nailuj05
nailuj05 / GetOrAddComponent.cs
Last active April 25, 2024 13:46
A simple extension method for Unity C# this gets or adds a given Component. (SphereCollider coll = gameObject.GetOrAddComponent<SphereCollider>();)
public static class Extensions
{
public static T GetOrAddComponent<T>(this GameObject gameObject) where T : Component
{
if(gameObject.TryGetComponent<T>(out T t))
{
return t;
}
else
{
@nailuj05
nailuj05 / LineCollider.cs
Created September 9, 2021 21:01
A simple way to add a collider to a Unity LineRenderer, intended for 2D use, can be adapted to 3D aswell!
using UnityEngine;
[RequireComponent(typeof(BoxCollider2D), typeof(LineRenderer))]
public class LineCollider : MonoBehaviour
{
private LineRenderer lineRenderer;
private BoxCollider2D boxCollider;
void Awake()
{