Skip to content

Instantly share code, notes, and snippets.

View MisterKidX's full-sized avatar

Dor Ben Dor MisterKidX

View GitHub Profile
@MisterKidX
MisterKidX / SelfCreatingPrefab.cs
Last active August 11, 2024 13:18
A gameobject that behaves differently whether it is a prefab or a runtime object.
using UnityEngine;
public class SelfCreatingPrefab : MonoBehaviour
{
private int fieldA;
private int fieldB;
public SelfCreatingPrefab Init(int arg1, int arg2)
{
if (!gameObject.scene.IsValid())
@MisterKidX
MisterKidX / CopyCompnent.cs
Last active October 30, 2025 11:50
A reflection method for copying components in Unity
public static T CopyComponent<T>(T component, GameObject destination) where T : Component
{
var type = component.GetType();
var copy = destination.AddComponent(type);
var fields = type.GetRuntimeFields();
// component, gameobject, etc. will override the name and tags, we don't want that
var properties = type.GetRuntimeProperties().Where(p => p.SetMethod != null && p.GetGetMethod(false) != null &&
p.DeclaringType != typeof(GameObject)
&& p.DeclaringType != typeof(Component)
&& p.DeclaringType != typeof(UnityEngine.Object)).ToArray();
@MisterKidX
MisterKidX / MonoBehaviourSingletonBase.cs
Last active November 3, 2025 15:14
added documentation and logs
using UnityEngine;
/// <summary>
/// provides an easy way to create singletons in unity based on monobehaviours
/// </summary>
/// <typeparam name="T">T must be the same type as the declaring type</typeparam>
public class MBSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
protected static T _instance;