I apologize for not being clear. I had, at that point, been coding for 11 hours plus so I was a bit zoned to say the least. I want to have several buttons that can “fire” or “spawn” a spell where the spell can be changed (the logic behind the skill bar on a mmo). Spells have different mana cost and casting times. I was up for another couple hours and figured it out. I don’t think it’s the most elegant solution but it works:
Within each spell prefab I have a spellController which holds the spells color, mana cost, and total cast time:
var manaCost: float = 1;
var castTimeTotal : float = 2.0;
var spellType: SpellColor;
enum SpellColor { Red, Blue, Green, Black, White }
Attached to the player I have the script HealthandMana which holds the players mana pools:
var playerManaBlue : float = 5;
var playerManaRed : float = 5;
var playerManaGreen : float = 5;
var playerManaWhite : float = 5;
var playerManaBlack : float = 5;
also in the player is the spellCasting which has several vars for several fire buttons. They are set to Transform so I can place any spell prefab into the slot. I have three extra variables: currentManaNeed is how much the spell cost to cast, currentManaHave is how much mana of that color I have, and colorHold which holds the color of the currentManaHave. Since the objects haven’t been spawned yet, I created an array which holds all the spell prefabs so I can access the necessary variables.
var SpellBody1 : Transform ;
var SpellBody2 : Transform ;
var SpellBody3 : Transform ;
var SpellBody4 : Transform ;
var SpellBody5 : Transform ;
var castTime : float = 0.0;
var totalCastTime : float;
var bIsCasting : boolean = false;
var prefabs : Transform[];
var currentManaNeed : int = 0;
var currentManaHave : int = 0;
var colorHold : colorType ;
enum colorType { Black, Blue, Green, Red, White }
I than had to assign the variables to the appropriate values using GetComponent in several different ways ( I gotta be honest I’m rather proud of myself for this one )
function castSpellone (){
totalCastTime = (SpellBody1.GetComponent(spellController).castTimeTotal);
if ( SpellBody1.GetComponent(spellController).spellType == SpellColor.Black) {
currentManaNeed = SpellBody1.GetComponent(spellController).manaCost;
currentManaHave = GetComponent(HealthandMana).playerManaBlack;
colorHold = colorType.Black;
}
else if ( SpellBody1.GetComponent(spellController).spellType == SpellColor.Blue) {
currentManaNeed = SpellBody1.GetComponent(spellController).manaCost;
currentManaHave = GetComponent(HealthandMana).playerManaBlue;
colorHold = colorType.Blue;
}
..................
Next I had check to see if the player has enough mana:
if (currentManaNeed > currentManaHave){
Debug.Log("Not enough" + SpellBody1.GetComponent(spellController).spellType + "Mana");
return;
}
After that its getting the cast time and using it in a while statement. Instantiating the spell, than resetting cast time:
while ( castTime < SpellBody1.GetComponent(spellController).castTimeTotal ) {
bIsCasting = true;
yield WaitForSeconds(0.1);
castTime += 0.1;
}
var newSpell = Instantiate (SpellBody1, Camera.main.transform.TransformPoint(Vector3.forward * 2), Camera.main.transform.rotation);
takeMana ();
bIsCasting = false;
castTime = 0;
}
the takeMana() function takes the mana from the proper mana pool in the player using the color hold variable:
function takeMana () {
if ( colorHold == colorType.Black){
GetComponent(HealthandMana).playerManaBlack -= currentManaNeed;
currentManaNeed = 0;
return;
}
else if ( colorHold == colorType.Blue){
GetComponent(HealthandMana).playerManaBlue -= currentManaNeed;
currentManaNeed = 0;
return;
}
else if ( colorHold == colorType.Green){
GetComponent(HealthandMana).playerManaGreen -= currentManaNeed;
currentManaNeed = 0;
return;
}
else if ( colorHold == colorType.Red){
GetComponent(HealthandMana).playerManaRed -= currentManaNeed;
currentManaNeed = 0;
return;
}
else if ( colorHold == colorType.White){
GetComponent(HealthandMana).playerManaWhite -= currentManaNeed;
currentManaNeed = 0;
return;
}
}
I left out the casting functionality as it was pertinent to the topic. I know it works as I’ve tested it out with several different spells that have different cast times and mana cost. Thanks for letting me sound board and this code above works with minor tweaking since I didn’t post all the else if possibilities.
Now how can I put solved in the title of this thread?