- Home /
[Help!]Unity FPS Health script error.
I want to give and receive damage from my AI
This is my AI`s gun script
IEnumerator ApplyDamage()
{
//Reduce the enemy's health
//Does NOT travel up the heirarchy.
if (hit.transform.tag != friendlyTag)
{
//Uncomment for RFPS
if (hit.collider.gameObject.GetComponent<HealthingScript>())
{
hit.collider.gameObject.GetComponent<HealthingScript>().PlayerDamage(damage);
}
hit.collider.SendMessage(damageMethodName, damage, SendMessageOptions.DontRequireReceiver);
}
//Produce the appropriate special effect
if (hit.transform.tag == hitEffectTag && hitEffect)
{
GameObject currentHitEffect = (GameObject)(Instantiate(hitEffect, hit.point, myTransform.rotation));
GameObject.Destroy(currentHitEffect, hitEffectDestroyTime);
}
else if (missEffect)
{
GameObject currentMissEffect = (GameObject)(Instantiate(missEffect, hit.point + hit.normal * 0.01f, Quaternion.LookRotation(hit.normal)));
GameObject.Destroy(currentMissEffect, missEffectDestroyTime);
}
this.enabled = false;
yield return null;
//Wait a fram to apply forces as we need to make sure the thing is dead
if (hit.rigidbody)
hit.rigidbody.AddForceAtPosition(myTransform.forward * bulletForce, hit.point, ForceMode.Impulse);
//Linger around for a while to let the trail renderer dissipate (if the bullet has one.)
Destroy(gameObject, timeToDestroyAfterHitting);
}
My player`s health script`s name is HealthingScript.js and there is ApplyDamage method like this
function PlayerDamage (damage : int) { if (hitPoints < 0.0) return;
hitPoints -= damage;
aSource.PlayOneShot(painSound, 1.0);
t = 2.0;
if (hitPoints <= 0.0) Die();
}
and it show me error like this UnityEngine.Component' does not contain a definition for HealthingScript and no extension method HealthingScript of type `UnityEngine.Component' could be found. Are you missing an assembly reference?
Any comment and answer is appreciated here. Thanks in Advance.
Is the name of class of HealthingScript.cs = HealthingScript ?
@Search HealthingScript is the name of script. Plz help!
I'm trying to replicate the same error message, but no luck. I thought it could be because of the use of some deprecated variable, or the name of the class on the .cs be different from the name of the .cs But the error messages they return are different than the one you posted.
Can you post the whole error?
So we can easily check which line the error is calling.
Also the whole code of HealthingScript please.
You are also using JavaScript, right?
@jchester07 yes you are right my gun script have written into javascript and my Ai gun script is written into C#
wait. I thought Search was assu$$anonymous$$g it's a cs script.
but you said your player's health script's name is HealthingScript*.cs*
but why are you using function PlayerDamage (damage: int). That's a JavaScript Syntax.
function PlayerDamage (damage: int). this is my player health script (HealthingScript written into javascript).
@jchester07 Sorry by mistake I had written that my HelathingScript is written in to c# but no my script is written into javascript.
So you're in a C# class trying to acess a JS script? if that's so, take a look at this answer and you'll find your solution.
https://answers.unity.com/questions/48874/accessing-javascript-variable-from-c-and-vice-vers.html
Answer by JunaidGhani · Nov 23, 2017 at 07:28 AM
Well It's not a condition i think -> if (hit.collider.gameObject.GetComponent())
you are getting HealthingScript component of the hitted object, thats all.
Use something like this:
if(hit.collider.gameobject.tag.Equals ( ("Player") || ("Enemy") )
{
hit.collider.gameObject.GetComponent().PlayerDamage(damage);
}
@JunaidGhani I didn`t understand plz write it in the form of code. Properly
Your answer
Follow this Question
Related Questions
Draw call minimizer 1 Answer
"Only assignment, call, increment, decrement and new object expressions can be used as statements" 1 Answer
Reference DLL's 1 Answer
MY FPSE`S fps is not moving need help! I tried all solutions! 0 Answers
How to follow a target (prefab) with the visual scripting: Behavior machine Pro? 0 Answers