How do I structure my classes to call one function from different scripts?

It’s hard to describe the question properly in the title, so here it is in more detail:

I have a class called CollisionPacket. It’s nothing more than a bunch of public class fields.

I (want to) have a function called CheckForCollision(tris, position, velocity) which takes as input a list of triangles, a position, and a velocity, and outputs a CollisionPacket object with newly calculated collision info.

  1. How do I structure my classes/scripts so that in any script I can write:
    CollisionPacket collPack = new CollisionPacket(); anywhere I need ?

  2. How do I structure my classes/scripts so that I only define CheckForCollision() in one place, and many different objects (with their different meshes) can call it?

Thanks :slight_smile:

  1. You can already do this without modification if your Class is Public. That’s how OOP works. You can create instances of public classes anywhere in your project.
  2. One way to do this is to make your CheckForCollision() method Static. C# static Keyword - Dot Net Perls

Just to answer for anyone who reads, I actually didn’t know you could just have scripts that weren’t attached to objects. Sounds simple but there you go.

So I have a script that is something like this:

using UnityEngine;

class CollisionPacket
    public Vector3 eRadius; // ellipsoid radius

    // Information about the move being requested: (in R3)
    public Vector3 R3Velocity;
        ... ...

Similar thinking for a class to contain CheckForCollision(). Maybe I would make that function static too, but haven’t tried things yet.

Note that it doesn’t use Monobehaviour. It’s not attached to any object, so it doesn’t need to.