CTIN 289 - Interactive Snippets Library

Powered by 🌱Roam Garden

CheckForGem.cs


Check for Gem was used to determine whether or not Mok was holding a specific object. Interestingly, it checks for a GRANDCHILD object using transform.Find() and a specific string. Very useful in our situation.


using UnityEngine;

public class CheckForGem : MonoBehaviour
{
    // This is a grandchild, which is why we use the forward slash!
    private string skull = "MorphObject/Skull";
    private string gem = "MorphObject/Gem";

    public bool hasSkull = false;
    public bool hasGem = false;

    private void Update()
    {
        hasSkull = CheckForItem(skull);
        hasGem = CheckForItem(gem);
    }

    public bool CheckForItem(string childObjectName)
    {
        Transform childTransform = transform.Find(childObjectName);

        if (childTransform != null)
        {
            GameObject childGameObject = childTransform.gameObject;
            bool isActive = childGameObject.activeInHierarchy;
            return isActive;
        }
        else
        {
            return false;
        }
    }
}