Search Unity

Notify other gameobject it has been hit by raycast2d

Discussion in 'Scripting' started by xqtr123, Sep 15, 2018.

  1. xqtr123

    xqtr123

    Joined:
    Jun 7, 2014
    Posts:
    20
    Hi!
    I'm using a mvc structure by thelab. If I shoot with my hero and hit an enemy I want to notify that enemy about the hit.

    As of now my View-script has the following code:

    WeaponView.cs
    Code (CSharp):
    1. public void Shoot
    2. {
    3.       //...
    4.       if (hit.collider != null)
    5.       {
    6.             Notify("enemy.hit", hit.transform.gameObject);
    7.       }
    8. }
    9.  
    EnemySoldierController.cs
    Code (CSharp):
    1.  
    2. //...
    3. public override void OnNotification(string p_event, Object p_target, params object[] p_data)
    4. {
    5.       switch (p_event)
    6.       {
    7.                    
    8.        case "enemy.hit":
    9.        var other = (GameObject) p_data[0];
    10.        Debug.Log("EnemySoldierController: Enemy hit - " + other)
    11.        
    12.        break;
    13.        }
    14. }
    15.  
    The enemy gameobject has three gameobjects attached to it, enemy soldier controller -> enemy soldier model -> enemy soldier view. The enemy soldier view gameobject has a HitBoxView.cs with a Die() method I want to call. How do I do this?

    I want something like this:
    Code (CSharp):
    1.  
    2.      other.GetComponent<HitBoxView>.Die()
    3.  
     

    Attached Files: