- Home /
How to create/fix fire damage script????
I have this script i have written to make the fire damage my player but it comes up with an error saying i need to swap the "=" (25,9) with a ":" instead but this leads to more problems please help!!!
var HealthAmount:int = 1 ;
var InFire:boolean = false;
var Interval:int = 1;
private var FPSInputController:GameObject;
function start ()
{
FPSInputController = GameObject.FindGameObjectWithTag("First Person Controller");
}
function Update ()
{
if(InFire)
{
HealthScript.Health -= HealthAmount;
yield WaitForSeconds (Interval);
}
}
function OnTriggerEnter(other:Collider)
{
if (other.GameObject == FPSInputController);
{
InFire=true
}
}
function OnTriggerExit(other:Collider)
{
if (other.GameObject == FPSInputController);
{
InFire=false
}
}
Answer by Graham-Dunnett · Jun 22, 2013 at 09:04 PM
Line 23 in the code above has a semi-colon at the end of the line. It shouldn't have. You should have a semi-colon at the end of 25. (And the same for lines 31 and 33).
Thanks that fixed my problem but it now has the error:
Script error: Update() can not be a coroutine.
Please Give me a fix and explain why this error occured
Edit: just realised the "WaitForSeconds (Interval)" is causing the problem but what can i use as an alternative to this.
Basically, you cannot use yield in Update.
Update is not a Coroutine.
A coroutine is a function that can suspend its execution (yield) until the given given YieldInstruction finishes.
Update cannot do this (again, it's not a Coroutine).
You could either change your health to float and use Time.deltaTime :
HealthScript.Health -= HealthAmount * Time.deltaTime;
or call a function that has the yield, but you'll need to create a boolean so that the yield function is not called every update.
pseudocode :
private var isYielding : boolean = false;
function Update()
{
if ( InFire && !isYielding )
{
ImOnFire();
}
}
function ImOnFire()
{
isYielding = true;
HealthScript.Health -= HealthAmount;
yield WaitForSeconds (Interval);
isYielding = false;
}
Thank you so much this script is now working perfectly fine once again thanks so much
Your answer
Follow this Question
Related Questions
Enemy Health Damage 2 Answers
My health decreases to fast. 1 Answer
Damage script is screwed up...? what to do? 1 Answer
My bullet script doens't work. 1 Answer
My player HP are get decreased from several GameObjects that has is Trigger on 0 Answers