A downloadable Scripting Assets

Download NowName your own price

This is a single script with some static extension functions. Currently there are only a couple of things but I will be extending it with more as time goes on. Documentation is included that goes over what each function does.

Update 11-28-2020:

  • Extended functionality of the TryGetComponent  functions. You can now use them on a game object instead of just on attached  components.
  • Added GameObject[].IsActive function.

Current Function List

  • TryGetComponents
  • TryGetComponentInParent
  • TryGetComponentInChildren
  • TryGetComponentsInParent
  • TryGetComponentsInChildren
  • AddExplosionForce2D
  • LookAt2D
  • GameObject[].IsActive

If you have any suggestions for functionality to include or bugs to report just let me know in the comments. If reporting a bug let me know what version of Unity you're  using. This should be compatible with all recent versions of Unity.

Download

Download NowName your own price

Click download now to get access to the following files:

Utility.cs and Documentation 58 kB

Comments

Log in with itch.io to leave a comment.

thanks for these but the whole idea behind the TryGetComponent/s family of methods is to not allocate any memory. Using the memory allocated versions of those in your implementations defeat the whole purpose.

The purpose of the method here was to bring the call down to a single line since I used it in a few places in a project I was working on. I'm not sure how I could get the same effect without allocating either the game objects that are being checked or the components I'm pulling them from to memory. Any suggestions how to get around that?

Here is my version, it also does away with checking a unity object against null, which is also problematic (see https://blogs.unity3d.com/2014/05/16/custom-operator-should-we-keep-it/)

public static bool TryGetComponentInChildren<T>(this Component c, out T retVal) where T : Component {
    foreach(Transform t in c.transform) {
        if(t.TryGetComponent<T>(out retVal)) return true;
    }     retVal = null;
    return false;
}
(+1)

thank, this is very helpful