Switching elements in multiple arrays?
So I have 3 empty game objects as children of my player, each acting as a spawn point for different types of VFX. one for projectiles, one for spread, burst like effects (think flamethrower), and lastly one for top-down AOE like effects that are spawned according to a RaycastHit hitpoint.
Each effect is instantiated through different inputs, handled by separate scripts and housed in separate lists. For each type of effect I intend to have a few different elements (like fire, ice, and lightning). I started writing some code to switch between the elements in the list, here's a bit I wrote for switching the projectile elements:
public class SpawnVFXProjectile : MonoBehaviour
{
public List <GameObject> vfx = new List <GameObject>();
private Transform myTransform;
private GameObject effectToSpawn;
void Start()
{
effectToSpawn = vfx[0];
myTransform = transform;
}
void Update()
{
if (Input.GetButtonDown("vfxFire"))
{
effectToSpawn = vfx[0];
}
if (Input.GetButtonDown("vfxIce"))
{
effectToSpawn = vfx[1];
}
if (Input.GetButtonDown("Fire1"))
{
SpawnVFX();
}
}
This works great but I started thinking is there not a more efficient way to do this for all three lists without writing the same code in all three scripts? Is it efficient or possible to write a fourth script, attached to a fourth game object, that will gather all three lists and switch them all to the same element within a single if statement? Oh and by the way I initially thought about just toggling through the list but as a combat mechanic that seems pretty clunky to have to tap the 'tab' key or something 3 times to get to the element I need.
Answer by SageGlaze · Mar 16, 2019 at 07:56 AM
Almost forgot about this. I was hoping to find a solution involving delegates but I couldn't wrap my mind around it. I basically made my effectToSpawn a public variable in each of the scripts that handled the different effects and created a 4th script attached to my player gameObject that accessed each of the 3 scripts and switched the index each effectToSpawn variable was assigned to upon the specified input.
I unfortunately don't have the 4th script available anymore as I'm now using object pooling and I have no idea how to add a list to a list sorry :/ but it pretty much looked something like this:
public GameObject projectileSpawnPoint;
public GameObject spreadSpawnPoint;
public GameObject beamSpawnPoint;
private SpawnVFXProjectile projectileSpawner;
private SpawnVFXSpread spreadSpawner;
private SpawnVFXBeam beamSpawner;
void Start()
{
projectileSpawner = projectileSpawnPoint.GetComponent<SpawnVFXProjectile>();
spreadSpawner = spreadSpawnPoint.GetComponent<SpawnVFXSpread>();
beamSpawner = beamSpawnPoint.GetComponent<SpawnVFXBeam>();
}
void Update()
{
if(Input.GetButtonDown("vfxFire"))
{
projectileSpawner.effectToSpawn = vfx[0];
spreadSpawner.effectToSpawn = vfx[0];
beamSpawner.effectToSpawn = vfx[0];
}
if(Input.GetButtonDown("vfxIce"))
{
projectileSpawner.effectToSpawn = vfx[1];
spreadSpawner.effectToSpawn = vfx[1];
beamSpawner.effectToSpawn = vfx[1];
}
}