Group Opacity Controller | Child Opacity Controller | 2D & 3D | C# | Unity Game Engine


GroupOpacityController.cs (using Update)
using UnityEngine;

[ExecuteAlways]
public class GroupOpacityController : MonoBehaviour
{
    [Range(0f, 1f)]
    public float opacity = 1f;  
    Renderer[] renderers;
    SpriteRenderer[] spriteRenderers;

    float _previousOpacity;

    void Start()
    {
        renderers = GetComponentsInChildren<Renderer>();
        spriteRenderers = GetComponentsInChildren<SpriteRenderer>();
        SetOpacity(opacity);
    }

    void Update()
    {
        if(opacity != _previousOpacity)
        {
            SetOpacity(opacity); 
            _previousOpacity = opacity;
        }
    }

    void SetOpacity(float opacity)
    {
        if (renderers != null)
        {
            foreach (Renderer renderer in renderers)
            {
                for (int i = 0; i < renderer.sharedMaterials.Length; i++)
                {
                    Color color= renderer.sharedMaterials[i].color;
                    color.a = opacity;
                    MaterialPropertyBlock matBlock = new MaterialPropertyBlock();
                    matBlock.SetColor("_Color", color);
                    renderer.SetPropertyBlock(matBlock, i);
                }
            }
        }

        if (spriteRenderers != null)
        {
            foreach (SpriteRenderer spriteRenderer in spriteRenderers)
            {
                Color color = spriteRenderer.color;
                color.a = opacity;
                spriteRenderer.color = color;
            }
        }
    }
}
GroupOpacityController.cs (using Property)
using UnityEngine;

[ExecuteAlways]
public class GroupOpacityController : MonoBehaviour
{
    [SerializeField, Range(0f, 1f)]
    float _opacity = 1f;
    public float Opacity
    {
        get { return _opacity; }
        set
        {
            _opacity = Mathf.Clamp01(value);
            SetOpacity(_opacity);
        }
    }    

    Renderer[] renderers;
    SpriteRenderer[] spriteRenderers;
	
#if UNITY_EDITOR
    void OnValidate()
    {
        SetOpacity(_opacity);
    }
#endif

    void Start()
    {
        renderers = GetComponentsInChildren<Renderer>();
        spriteRenderers = GetComponentsInChildren<SpriteRenderer>();
        SetOpacity(_opacity);
    }

    void SetOpacity(float opacity)
    {
        if (renderers != null)
        {
            foreach (Renderer renderer in renderers)
            {
                for (int i = 0; i < renderer.sharedMaterials.Length; i++)
                {
                    Color color= renderer.sharedMaterials[i].color;
                    color.a = opacity;
                    MaterialPropertyBlock matBlock = new MaterialPropertyBlock();
                    matBlock.SetColor("_Color", color);
                    renderer.SetPropertyBlock(matBlock, i);
                }
            }
        }

        if (spriteRenderers != null)
        {
            foreach (SpriteRenderer spriteRenderer in spriteRenderers)
            {
                Color color = spriteRenderer.color;
                color.a = opacity;
                spriteRenderer.color = color;
            }
        }
    }
}

Input Cooldown | Input Delay | C# | Unity Game Engine


InputCooldown.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InputCooldown : MonoBehaviour
{
    public float cooldownTime = 0f;
    private bool isCooldown = false;

    private void Update()
    {
        if (Input.GetMouseButton(0) && !isCooldown)
        {
            StartCoroutine(Cooldown());
            SpawnSphere();
        }
    }

    private IEnumerator Cooldown()
    {
        isCooldown = true;
        yield return new WaitForSeconds(cooldownTime);
        isCooldown = false;
    }

    private void SpawnSphere()
    {
        GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
        sphere.transform.position = Camera.main.ScreenToWorldPoint(mousePos);
        sphere.AddComponent<Rigidbody>();
        GameObject.Destroy(sphere, 5);
    }
}

Simple Pickup and Inventory System | Pickup | Inventory | C# | Unity Game Engine


Item.cs
[System.Serializable]
public class Item
{
    public string name;
    public int count;

    public Item(string itemName, int itemCount)
    {
        name = itemName;
        count = itemCount;
    }
}

Inventory.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Inventory : MonoBehaviour
{
    public static Inventory instance;
    public List<Item> items = new List<Item>();

    void Awake()
    {
        if (instance != null)
            Destroy(gameObject);
        else
            instance = this;
    }

    public void AddItem(Item itemToAdd)
    {
        bool itemExists = false;

        foreach (Item item in items)
        {
            if(item.name == itemToAdd.name)
            {
                item.count += itemToAdd.count;
                itemExists = true;
                break;
            }
        }
        if(!itemExists)
        {
            items.Add(itemToAdd);
        }
        Debug.Log(itemToAdd.count + " " + itemToAdd.name + "added to inventory.");
    }

    public void RemoveItem(Item itemToRemove)
    {
        foreach (var item in items)
        {
            if(item.name == itemToRemove.name)
            {
                item.count -= itemToRemove.count;
                if(item.count <= 0)
                {
                    items.Remove(itemToRemove);
                }
                break;
            }
        }
        Debug.Log(itemToRemove.count + " " + itemToRemove.name + "removed from inventory.");
    }
}


Pickup.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pickup : MonoBehaviour
{
    public Item item = new Item("Item Name", 1);

    void OnTriggerEnter(Collider other)
    {
        if(other.CompareTag("Player"))
        {
            Inventory.instance.AddItem(item);
            Destroy(gameObject);
        }
    }
}

Print message in Console Log using Script Graph | Debug Log | Visual Scripting | Unity Game Engine


Lifecycle events and their execution order in a script graph | Visual Scripting | Unity Game Engine


Decorate console log messages | Debug.Log | C# | Unity Game Engine


MouseClickCounter.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseClickCounter : MonoBehaviour
{
    int clickCount = 0;

    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            Debug.Log($"<i>click</i> <b>count</b> <size=20>=</size> <color=cyan>{++clickCount}</color>");
        }
    }
}