create a function so i dont have to paste it into everything

is there a way i can create a function just in general that i can call for any gameobject i decide to use it with?

like, for example, if i want the player gameobject and every enemy gameobject to have a piece of code that checks their destination for a certain obstacle (let’s call it a landmine for argument’s sake) before moving, can i just write a “checkmines” script one time or do i need to paste a code that says something to the effect of

“if overlapsphere on target floor doesn’t return a mine, go ahead and step there”

in every type of enemy i make?

i have more general applications i’d prefer to use this for as well this just seemed like an easy example

thanks!

You could make a static method, passing into that method all things you want to evaluate as parameters.

public class CustomUtils{
    public static void MyFunctionality(Transform p1, Transform p2, /*...*/){
        //whatever you need to do with parameters.
    }
}

//calling function
CustomUtils.MyFunctionality(transform, enemyTransform,/*...*/);

If you want a deeper dive into something more advanced that is this idea in spades, look into using DOTS

1 Like