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?
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
– unity_8B8891DEC429FD479581Yes. 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.
– Pangamini