Two functions wont cooperate.

Hello, I’ve been having trouble with this single piece of code for a long time, and It’s been really messing with my head.
ApplyManaLoss is called for in a different function that dosen’t matter much in this problem (I think), and totalmanacost can be either 25, 50, or 75 (part of the game), normally total manacost is in a different script, but for the purposes of this post I moved it into the script below.

var manastandin = 100.0;
var regen = 10;
var fullmana = 100;
var totalmanacost = 25;

function Update()
{
Manatick();
}

function ApplyManaLoss (totalmanacost)
{
manastandin -= totalmanacost;
}

function Manatick()
{
if (manastandin < fullmana)
{
manastandin = (regen * Time.time);
Debug.Log(manastandin);
}
}

Ok,so adding to manastandin works just fine, but if manastandin isn’t full, the ApplyManaLoss does nothing. If mana is at full, then it adds (rather than subtracts) about 10 to the manastandin. If manastandin is a few hundred higher than full mana, it subtracts from manastandin properly, untill it gets to close to fullmana, then it adds again.
Changing the regen variable changes how close manastandin cares to get to fullmana.
I think it has to do with the script valuing the Manatick more than the ApplyManaLoss, and ignoring it unless manatick is inactive.
any help I could get would be appreciated, thank you.

Your ApplyManaLoss function takes an argument, so seeing the context in which you are calling it is pretty important.

Also, adding a Debug.Log(totalmanacost); to the ApplyManaLoss function would be a quick way to see if anything is going wrong on that side.

manastandin = (regen * Time.time);

Shouldn’t it be :

manastandin += (regen * Time.deltaTime);

Ok, so the code which triggers the mana loss is a bit large, but here it is. Points of intrest are the totalmanacost(near the top), and the applymanaloss(near the bottom).

var manacost = 25;
var casttime = 1;
var castnumber = 0;
private var _keyPressedFire : boolean = false;
private var _keyPressedOne : boolean = false;
var mana = 100.0;
var totalmanacost = 0;

function Start()
{
SpellPrep("1");
SpellReady("Fire2");
}

//first is 25, next 50, next 75, linear spell cost system
function CurrentSpellCost()
{
if (castnumber <= 1)
{
totalmanacost = manacost;
}
if (castnumber > 1)
{
totalmanacost = castnumber * manacost;
}
}

//trigger of the whole thing
function SpellPrep(_button : String)
{
while(!_keyPressedOne)
{
CurrentSpellCost();
if(Input.GetKeyDown(_button)  castnumber < 3  mana>= manacost * castnumber + manacost)
{
_keyPressedOne = true;
Debug.Log("Cast bar here"); 
castnumber +=1;
Debug.Log(castnumber);
_keyPressedOne = false;
}
yield;
}
}

//spell is prepared, fire trriggers spell
function SpellReady(_button : String)
{
while(!_keyPressedFire)
{
if(Input.GetButtonDown(_button)  castnumber > 0)
{
CastSpell();
}
yield;
}
}


function ApplyManaLoss ()
{
mana -= totalmanacost;
}

//goes and gets functional portion of spell
function CastSpell () {
_keyPressedFire = true;
var spellone = GetComponent(SpellOne);
spellone.CastSpellOne();
SendMessageUpwards("ApplyManaLoss", totalmanacost,
SendMessageOptions.DontRequireReceiver);
_keyPressedFire = false;
castnumber = 0;
}

It’s grown a bit strange as I’ve went along, so sorry if it’s kinda primitive.