Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by 2dfruity · May 19, 2014 at 06:02 AM · listsendmessageremoveatgeneric.list

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);
                     }
             }
     }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · May 19, 2014 at 09:06 AM 1
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges