Hello sorry! I’ve managed to figure out my own solution before i saw your comments @andrew-lukasik . I’ve changed the modifiers to be MonoBehaviours. So i’ve got my base Modifier class
public class Modifier : MonoBehaviour, IModifier
{
public virtual void OnDecay(Transform transform)
{
Debug.Log("default");
}
}
Which just contains the default on decay method now, and instead i’ve opted to create seperate scripts that derive from modifier. I’ve got a list of those scripts in string form within my GameAssets script. Now, whenever i call the ShootPattern method, it takes a string in place of a modifier. It then uses this string to parse through the list in GameAssets to find if its a valid script and add the component to the bullet itself, whilst also giving the bullet a reference to the component. bullet calls that component on death etc etc. Component gets deleted along with bullet afterwards.
Heres the updated sections of the Spawner script
public void ShootPattern(Transform origin, Projectile projectile, ProjectilePattern pattern, string mod = null, bool temp = false)
{
//randomize the pattern if it needs to be
if (pattern.random)
{
for (int i = 0; i < pattern.projCount; i++)
{
pattern.projDir *= new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f));*
Debug.Log("Randomized dir - " + pattern.projDir*);*
pattern.projAngle = Random.Range(0f, 1f);
Debug.Log(pattern.projAngle*);*
}
}
StartCoroutine(Shoot(pattern, projectile, origin, mod, temp));
}
IEnumerator Shoot(ProjectilePattern pattern, Projectile projectile, Transform origin, string mod, bool temp)
{
for (int i = 0; i < pattern.projCount; i++)
{
//Wait for the delay time.
yield return new WaitForSeconds(pattern.projTimes*);*
//instantiate new projectile and grab reference to it.
var proj = Instantiate(projectilePrefab);
//If the modifier name is valid, Add the modifier to the bullet, else add the default modifier.
if (GameAssets.I.modifiers.Contains(mod))
{
Debug.Log("mod " + mod);
proj.gameObject.AddComponent(Type.GetType(mod));
proj.GetComponent().modifier = (Modifier)proj.GetComponent(Type.GetType(mod));
}
else
{
Debug.Log(“Default mod”);
proj.gameObject.AddComponent();
}
//setting the movement direction of the bullet
var forward = origin.forward;
Vector3 direction = new Vector3(forward.x + pattern.projDir.x, forward.y + pattern.projAngle_, forward.z + pattern.projDir*.y - 1);
proj.transform.position = origin.position + direction.normalized;
Debug.Log("direction = " + direction.normalized);
proj.GetComponent().velocity = direction.normalized;
proj.GetComponent().currentProjectile = projectile;
}*_
if (temp)
{
Destroy(gameObject);
}
}
Its a bit roundabout but i’m quite happy with it overall. Thanks for your help anyways.