Apologies in advance for the lengthy introduction, I’ve tried my best to describe my situation as succinctly as possible.
I have a non-Monobehaviour class called Spell which defines the properties of my spells:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spell
{
public string name;
public GameObject missilePrefab;
public int missileDamage;
public float missileForce;
public float missileLifetime;
public int missileRichochets;
// etc.
}
I then have another Monobehaviour script (called SpellDatabase.cs) which uses the Spell class to create different types of spells with unique properties. This script is attached to an empty gameobject, ‘SpellDatabase’ within the scene.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpellDatabase : MonoBehaviour
{
private static SpellDatabase _instance;
public static SpellDatabase Instance
{
get
{
if (_instance == null)
Debug.Log("The SpellDatabase is NULL.");
return _instance;
}
}
private void Awake()
{
_instance = this;
fire = CreateSpell(
"Fire",
Resources.Load("!Prefabs/Attacks/Missiles/1-Form/MissileFire") as GameObject,
15,
1500,
5,
0);
ice = CreateSpell(
"Ice",
Resources.Load("!Prefabs/Attacks/Missiles/1-Form/MissileElectricity") as GameObject,
20,
1100,
5,
0);
water= CreateSpell(
"Water",
Resources.Load("!Prefabs/Attacks/Missiles/1-Form/MissileWater") as GameObject,
10,
800,
10,
0);
}
private Spell CreateSpell(
string name,
GameObject missilePrefab,
int missileDamage,
float missileForce,
float missileLifetime,
int missileRichochets)
{
var spell = new Spell(
name,
missilePrefab,
missileDamage,
missileForce,
missileLifetime,
missileRichochets);
return spell;
}
I then have a number of projectile prefabs which each contain a Monobehaviour script, MissileProjectile.cs whose purpose is to assign the prefabs with their appropriate properties such as damage, force etc. during void Start() (i.e. as soon as they are instantiated). e.g. The fire projectile prefab should get the SpellDatabase.fire spell instance and use its properties.
The problem I’m having is how to go about assigning the spell instances (e.g. SpellDatabase.fire, SpellDatabase.ice, SpellDatabase.water) to the MissileProjectile.cs scripts on each of the projectile prefabs. Ideally I’d like there some kind of drop-down list displayed on the Unity Inspector for MissileProjectile.cs, enabling me to assign the matching spell instance to each of the projectile prefabs.
I know how to code a reference to a specific spell instance however I do not want to have multiple copies of the MissileProjectile.cs script, each referencing a specific spell instance.
Thanks for reading. How can I achieve this?