Need make a script reference without specifying script?

My game has a particle object as a prefab. The particle is initialized by another GameObject which makes multiple at a time and needs to be able to reference the GameObject of the particle (this is used to destroy the particle after some conditions are met) and the script attached to that particle (which contains a function that needs to be called semi-frequently).

I don’t know how inefficient it would be to just use GetComponent every time I wanted to access the script but I imagine it isn’t the best, so I have two lists, one storing the object reference and the other storing the script reference of each object, and then I realized I could maybe make another class that has both of these as properties and make a list of that.

public class ParticlePackage
{
    GameObject myParticle;
    ParticleScript myScript;
    public ParticlePackage(GameObject particle)
    {
        myParticle=particle;
        myScript=particle.GetComponent<ParticleScript>();
    }
}

This seems to work, but I realized that I may also have use for a similar class in the rest of my project, so I started making the code more general

public class ObjectPackage
{
    public GameObject myObject;
    public ParticleScript myScript;
    public ObjectPackage(GameObject object)
    {
        myObject=object;
        myScript=object.GetComponent<ParticleScript>();
    }
}

But I realized that I don’t know what to change ParticleScript to, is there a generic script type that I can assign any script to to allow it to work with different GameObjects?

2 Answers

2

Why can’t you just store references to ParticleScript instances?
You don’t need to ever call GetComponent. Use the script type for prefab field as well. To destroy, call Destroy(particle.gameObject);

I need to call the method SetPosition() from the ParticleScript on update. this method sets the particles position, which is calculated using several variables which are set upon the instantiation of the particle, as well as a variable that is given as a parameter by the script managing all the particles. for this reason, I need to be able to reference the particle script very often, and I feel that storing the script references would be more efficient than calling GetComponent every frame, but I may be wrong

–

Yes. So just do it, right? You don't need the Package class, just reference the ParticleScript directly. You don't need to store a reference to gameObject.

–

If I understand your question correctly, it sounds like you’re looking for a way to create a variable that can store any kind of script you’ve made so it’s easier for you to reference them on the attached GameObject. While I’m not aware of any way to create a “generic” script variable (if there is, I would also like to know about it) there are many ways you could go about accessing different types of scripts on a GameObject. The simplest way I can think of at the moment would be to just create variables for each script you’ve made that would be attached to a GameObject and then assign them when the GameObject gets enabled

public class ObjectPackage : MonoBehaviour
{
   public GameObject myObject;
   public ScriptA a;
   public ScriptB b;
   public ScriptC c;

    void Start()
{
   myObject = this.gameObject;
  if(GetComponent<ScriptA>() != null)
        {
            a = GetComponent<ScriptA>();
        }
 if(GetComponent<ScriptB>() != null)
        {
            b = GetComponent<ScriptB>();
        }
if(GetComponent<ScriptC>() != null)
        {
            c = GetComponent<ScriptC>();
        }

}
  etc....