Search Unity

SendMessage Function

Discussion in 'Scripting' started by moynzy, Nov 28, 2014.

  1. moynzy

    moynzy

    Joined:
    Oct 22, 2014
    Posts:
    82
    In my enemy Ai script I am using the sendMessage function to talk to scripts.

    It works perfectly, but when I duplicate the enemy with the ai script on, it appears that sendMessage() sends the message to the other enemy.

    For example.

    if(player hits enemy) sendMessage("enemyHasBeenHit").

    This sends the message to the other enemies. Which I don't want.

    Is there a fix for this>
     
  2. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    i dont know much about the send message system, but a other approach could be to use C# Events and Delegates, and just have your eneimies you want to recive subscribe to the event
     
  3. moynzy

    moynzy

    Joined:
    Oct 22, 2014
    Posts:
    82
    Unity is really killing my head lol.

    Normally in like a 2D flash game you'd have a "Main" script. But it's difficult on Unity c#. Since you attatch scripts to objects.

    for example, if you attack an enemy, you will want to decrease health.
    if(player attacks enemy)
    enemy.DecreaseHealth();

    How would you get access to that enemies health without affecting other enemies. It's confusing.
     
  4. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    557
    If you're used to the single-script approach I recommend reading up on Object-Oriented Programming (OOP). It's a *little* bit different in Unity because you also assign scripts to objects directly and graphically (rather than using new Object() only), but it should help you get in the right mindset for things. =)

    For something like what you're describing you almost certainly won't want to use SendMessage() either; you would call objects' methods directly.

    Some Google-terms to help get you started: OOP, encapsulation, access modifiers and perhaps instance members VS class members if you want to go that far right from the get-go.
     
  5. moynzy

    moynzy

    Joined:
    Oct 22, 2014
    Posts:
    82
    I appreciate the feedback, I've been following tutorials and reading on books and looking at API examples.
    Code (CSharp):
    1.     public void OnTriggerEnter(Collider other){
    2.  
    3.  
    4.         if(other.gameObject.name == ("HitBox"))
    5.         {
    6.             if(canAttackEnemy){
    7.  
    8.                 if(attack){
    9.  
    10.                     attack = false;
    11.            
    12.  
    13.                 }else{
    14.                     Debug.Log("no hit");
    15.                 }
    16.            
    17.             }
    18.         }
    19.     }
    I have a "EnemyHealth" script on this enemy that has been collided with.

    I want to access it's script so here is what I have done.

    Code (CSharp):
    1.     public void OnTriggerEnter(Collider other){
    2.  
    3.         GameObject target = other.gameObject;
    4.  
    5.         if(other.gameObject.name == ("HitBox"))
    6.         {
    7.             if(canAttackEnemy){
    8.  
    9.                 if(attack){
    10.  
    11.                     attack = false;
    12.                     EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
    13.                     eh.MinusHealth();
    14.            
    15.  
    16.                 }else{
    17.                     Debug.Log("no hit");
    18.                 }
    19.            
    20.             }
    21.         }
    22.     }
    However on collision I get a null reference error.
    The line is : eh.MinusHealth();

    Do you know why by any chance? Thanks.
     
  6. moynzy

    moynzy

    Joined:
    Oct 22, 2014
    Posts:
    82
    Please ignore me, I've been programming for 12 hours straight.

    I need to be slapped in the face.