Problem Calling Function from Other Script
So basically I have two scripts, the enemy health script that I attach to the enemy that goes as follows.
#pragma strict
var Health = 100;
var animDie : AnimationClip; // Drag your animation from the project view in here (to inspector)
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
if(Health <= 0)
{
Dead();
}
}
function Dead()
{
GetComponent.<Animation>().Play(animDie.name);
Destroy(this.gameObject, animDie.length);
}
I also have the script that I attach to my Bullet, that goes as follows... #pragma strict
function OnCollisionEnter (col : Collision)
{
if(col.gameObject.name == "char4_snipersnest_char4")
{
gameObject.SendMessage("ApplyDammage",100);
}
}
However, when they collide the console says the following: "SendMessage ApplyDammage has no receiver!" I've seen some other threads on the SendMessage has no Receiver problem but I can't quite follow them in my case. Any help here would be massively appreciated. Thanks.
Answer by Hethu · Aug 12, 2016 at 07:39 AM
I do not know if it works in JS but this is my C# Script for the Bullet/Laser:
I hope it will help somehow.
public class LaserDamage : MonoBehaviour {
public float damage = 1;
void OnTriggerEnter2D (Collider2D other)
{
if (!other.CompareTag ("Player"))
{
other.SendMessage("ApplyDamage",damage,SendMessageOptions.DontRequireReceiver);
Destroy(gameObject);
}
}
}
Answer by Aeleck · Aug 12, 2016 at 02:18 PM
Actually as I look at your Bullet script @KaiserDE, you are asking the Bullet script for the method ApplyDamage when it's actually you're enemy that has that method.
function OnCollisionEnter (col : Collision)
{
if(col.gameObject.name == "char4_snipersnest_char4")
{
//asks the bullet script for the method ApplyDammage (no method exists)
gameObject.SendMessage("ApplyDammage",100);
//the below should work since the method you want is on the enemy object
col.gameObject.SendMessage("ApplyDammage",100);
}
}
Your answer
Follow this Question
Related Questions
Enemy clones not destroying after a certain number of clones. 0 Answers
How To Destroy/Deactivate one enemy without affecting other enemies of the same type? 0 Answers
My enemies' health wont go down 0 Answers
If one prefab enemy dies, the other enemy prefabs malfunction 0 Answers
Player cannot destroy enemy 0 Answers