GameObject and Component Extensions

These extensions are quality of life features for Components and GameObjects. Currently there are only a few extensions, but in the future more features will make their way into Nut Library. These extensions are chainable.

EXTENSIONS

CompareParent

  • bool CompareParent(GameObject/Component other)

Compares the parents of both objects and returns true if they match eachother.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    public GameObject obj;

    void MyMethod()
    {
        if (this.CompareParent(obj))
        {
            // Do stuff…
        }
    }
}

Parent, Unparent, and SetActive

There are many variations of these extensions. They are chainable, as can be seen in the example below.

  • Parent(Transform parent)

Sets the object’s parent to “parent”.

  • Unparent()

Sets the object’s parent to the scene of the root, which is essentially “null”.

  • SetActive(bool state) - Component-Only

Sets the component’s GameObject’s active state.

  • ParentAndSetActive(Transform parent, bool state)

Sets both the object’s parent and active state.

  • UnparentAndSetActive(bool state)

Sets the object’s parent to «null» and the active state to «state».

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    public GameObject obj;
    public Transform parent;

    void MyMethod()
    {
        obj.Parent(parent);
        this.Unparent(); // "this" is a component in this case (MyClass), so it's also possible to use extensions like this.
        obj.ParentAndSetActive(transform, false);
        obj.UnparentAndSetActive(true);
        parent.SetActive(true); // Transform is a component as well, so it's possible to call SetActive on it too.
        this.SetActive(false)
            .Parent(parent); // Chained command example.
    }
}

GET HELP

Join this Discord server to get help from the community, suggest new features, and vote on future updates!

See also

  • Array and List Extensions
  • GameObject and Component Extensions
  • Physics Extensions
  • Texture Extensions
  • Value Extensions
  • Transform.SetParent()
  • GameObject.SetActive()