- Home /
script runs fine on start up, but slows on second attempt
Hi,
I wondered if anyone could solve this for me? I have this script that runs perfect the first time I click play on the editor, but when it's completed it's cycle and I click the button again, it pauses for a bit. There is a loose, non-functional version of my code below.
I'm wondering if it's anything to do with turning the button off or the waitforseconds();. There is no stuttering at all, the animations play fine, they bob along in idle while waiting for the button to turn back on. It only turns on immediately after the attack when I turn off the editor and back on. If I do the cycle twice, it will pause before turning the buttons back on.
function ButtonStart()
{
currentState = State.PlayerTurn;
TurnOffButton();
StateManager();
}
function StateManager()
{
switch(currentState)
{
case State.PlayerTurn:
PlayerTurn();
break;
case State.EnemyTurn:
EnemyTurn();
break;
case State.Attack:
Attack();
break;
case State.SwitchTurn:
Switch();
break;
default:
break;
}
}
function PlayerTurn ()
{
// sets player meshes & tags
Attack();
}
function EnemyTurn ()
{
// sets enemy meshes & tags
Attack();
}
function Switch ()
{
// if turn is players then change turn to Enemy turn
// if turn is enemys then change turn to player turn
&& TurnOnButton();
}
function Attack ()
{
attacker.animation.Play("attack");
attacker.animation.PlayQueued("idle");
yield WaitForSeconds (attacker.animation["attack"].length + 0.2);
//Normal Damage Calculation
defender.animation.Play("takeDamage");
defender.animation.PlayQueued("idle");
yield WaitForSeconds (defender.animation["takeDamage"].length + 0.2);
text.text = "Dealt " + Damage + " damage!";
currentState = State.SwitchTurn;
StateManager();
}
TurnOffButton ()
{
button.interactable = false;
}
TurnOnButton ()
{
button.interactable = true;
}
$$anonymous$$aybe the fix I found for a similar issue I had (posted here) can help you?
Answer by juniperspark · Jan 31, 2015 at 05:16 PM
Hi there!
I found the problem! As i'd edited the code to be a stripped down version, I'd left out the part that was causing me problems, which was stupidly the Debug.Log. If I removed this from my script, it worked! Thanks!