Using variables from non-Instantiated objects

So Im coming along on my project alot faster than I had hoped and have accomplished much with the search function. However I am at an impasse.

I currently have a casting time set-up for 4 seconds whenever I Instantiate an object. After the spell is cast I take away mana of a specific color from the players “mana pool”. This one is black and takes away just because I have the black spell prefab in SpellBody1 that cost one black mana. This script is attached to the player.

var SpellBody1 : Transform ;

var castTimeTotal : float = 4.0;
var castTime : float = 0.0;

var bIsCasting : boolean = false;

function castSpellone (){
    
    if (GetComponent(HealthandMana).playerManaBlack == 0){
        
        Debug.Log("Not enough Black Mana");
        
        return;

    }

    else if ( bIsCasting == true){
            
        Debug.Log("Still in previous cast");

        return;
            
    }

    while ( castTime < 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);
    
    GetComponent(HealthandMana).playerManaBlack -= 1;

    bIsCasting = false;
            
    castTime = 0;

}

Now I want to create objects with different cast times and different mana cost. The first problem I see is accessing the information from the prefab. The second is how to, for lack of a better term, translate the mana cost and type on the spell with the “mana pools” in the player.

So I worked out how to subtract the mana from the “mana pools” based on the spell in SpellBody1 , now I just need to pull from the correct mana pool in the player controller based on a variable inside the spell. I hope this doesn’t bump.

dont undestand your question

Wait - I think I see what you’re asking about. You want a variable that you can access and call before you have an Object of that Type, right? I’m pretty sure you would use a static variable for that, created outside all of the methods of the class. Then you should be able to call the variable using he name of the class instead of the name of an object (i,e. instead of Fireball1.cost it would be FireballSpell.cost, where FireballSpell is the class name). I’m not sure of the exact reference operation, being new to Unity, but one of the other fine fellows here should be able to assist you. Alternatively, you could just store the cost values somewhere in your character object or wherever you’re doing the casting operation.

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?