- Home /
GameObject.SendMessage not working
i have an AI that uses an OnTriggerEnter to compile a list of enemies. In the enemy's script after the Destroy command there is a sendMessage, but it doesn't call the function to remove itself from the AI's list. Am I doing something wrong?
enemy's death:
void EnemyDamaged(int damage){
if (enemyHealth > 0){
enemyHealth -= damage;
}
if (enemyHealth <= 0) {
enemyHealth = 0;
enemyDead = true;
//gameManager.curEXP += 10;
Destroy(gameObject);
if(enemy == null){
goul.SendMessage ("KillTargetInList", null, SendMessageOptions.DontRequireReceiver);
}
}
}
and the AI:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Gouloriginal : MonoBehaviour {
public Enemy2D enemy2D;
public Transform target;
public Transform player;
public int moveSpeed;
private Transform goul;
public int idleDistance = 1;
public int enemyMaxDistance = 20;
public int enemyMinDistance = 2;
//attack vars
public float attackRange = 2;
float attackRate = 1f;
float coolDown = 1;
public int goulDamage = 10;
public List<Transform> myList;
//12+ hours on this fucking script...
void Awake(){
goul = transform;
}
void Start () {
myList = new List<Transform> ();
//GameObject to = GameObject.FindGameObjectWithTag ("Enemy");
//target = to.transform;
GameObject po = GameObject.FindGameObjectWithTag("Player");
player = po.transform;
}
void Update (){
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void FixedUpdate () {
if( target != null){
target = myList [0];
if (Vector3.Distance (goul.position, target.position) < attackRange) {
if (Time.time >= coolDown){
GoulAttack();
coolDown = Time.time + attackRate;
}
}
}
if (target == null || Vector3.Distance (goul.position, target.position) > enemyMaxDistance){
if (Vector3.Distance (goul.position, player.position) > idleDistance) {
if (player.position.x < goul.position.x) {
goul.position -= goul.right * moveSpeed * Time.deltaTime; // player is left of goul, move left
} else if (player.position.x > goul.position.x) {
goul.position += goul.right * moveSpeed * Time.deltaTime; // player is right of goul, move right
}
}
}
else if (target != null) {
if (Vector3.Distance (player.position, target.position) < enemyMaxDistance) {
if (Vector3.Distance (goul.position, target.position) > enemyMinDistance) {
if (target.position.x < goul.position.x) {
goul.position -= goul.right * moveSpeed * Time.deltaTime; // target is left of goul, move left
} else if (target.position.x > goul.position.x) {
goul.position += goul.right * moveSpeed * Time.deltaTime; // target is right of goul, move right
}
}
}
}
Debug.DrawLine(player.position, goul.position, Color.yellow);
if (target != null) {
Debug.DrawLine (target.position, goul.position, Color.green);
}
}
void KillTargetInList(){
myList.RemoveAt (0);
Debug.Log("enemy dead");
}
void OnTriggerEnter (Collider tripwire){
if (tripwire.tag == "Enemy") {
myList.Add (tripwire.transform);
target = myList [0];
}
}
private void GoulAttack(){
//find target with tag
if (target != null) {
if (target.gameObject.tag == "Enemy") {
//send damage call to enemy script
target.gameObject.SendMessage ("EnemyDamaged", goulDamage, SendMessageOptions.DontRequireReceiver);
}
}
}
}
I'd be to your benefit if you didn't just post a code dump and said you had a problem, and ins$$anonymous$$d actually explain your problem, as well as your current approach.
Answer by Noob_Vulcan · May 19, 2014 at 09:20 AM
well you are destroying your current gameObject 1st. SO the code below it will never get executed. Change it to
if(enemy == null){
goul.SendMessage ("KillTargetInList", null, SendMessageOptions.DontRequireReceiver);
}
Destroy(gameObject);
Your answer
Follow this Question
Related Questions
List.RemoveAt() Causing Huge Memory Allocation 2 Answers
A node in a childnode? 1 Answer
Destroy a gameobject from an index of a list, destroy all same gameobjects right after it, why ? 2 Answers
How to use list to add new items & function? 0 Answers
Adding an item to a Generic List copies the old one 1 Answer