Search Unity

Help needed with instance calling C# event and passing value

Discussion in 'Scripting' started by winterfive, Aug 21, 2018.

  1. winterfive

    winterfive

    Joined:
    May 21, 2018
    Posts:
    31
    Hi,

    To be honest, I'm not sure this is possible.

    I have a script (DroneActions) on a prefab (Drone) that needs to call an event so that the ProjectileManager class can FireMissle(). The event is passing a GameObject and Transform as well.

    FYI _gunTipTransform is a field and it is assigned in Start()

    Here's my code in DroneActions:
    Code (CSharp):
    1. public delegate void TargetAcquiredHandler(GameObject go, Transform t);
    2. public event TargetAcquiredHandler TargetAcquired;
    3.  
    4. private void Update()
    5.     {
    6.         LookAtPlayer();
    7.  
    8.         if (Time.frameCount % 50 == 0)
    9.         {
    10.             if (Physics.Raycast(_gunTipTransform.position, -_gunTipTransform.forward, out _hit, 125))
    11.             {
    12.                 if (_hit.transform.tag == "Player")
    13.                 {
    14.                     if (TargetAcquired != null)
    15.                     {
    16.                         TargetAcquired(this.gameObject, _gunTipTransform);
    17.                     }
    18.                 }
    19.             }
    20.         }...
    In ProjectileManager:
    Code (CSharp):
    1. private void OnEnable()
    2.     {
    3.         DroneActions.TargetAcquiredHandler += ShootMissle;
    4.     }
    I'm getting an error on DroneActions.TargetAcquiredHandler, "error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected".

    I've researched my error code and find that I need to use a new keyword. But that doesn't feel right in regards to this event. Is it possible to call a C# event from a prefab? If so, how? I've googled about prefabs and events but all I've found so far is "Use the Unity events system". I prefer the C# events as it is faster. Help! Thanks!
     
  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    335
    you're trying to assign the event subsciber to the delegate definition.
    either the event needs to be static or ProjectileManager needs a reference
    let's say it has the reference

    Code (CSharp):
    1. [SerializeField]
    2. DroneActions actions;
    3. private void OnEnable()
    4. {      
    5.     actions.TargetAcquired += ShootMissle;  
    6. }
    7.  
    also consider, that you could define the event like this:
    Code (CSharp):
    1. public event System.Action<GameObject, Transform> TargetAcquired;
    and then you don't need the delegate definition.
     
    winterfive likes this.
  3. winterfive

    winterfive

    Joined:
    May 21, 2018
    Posts:
    31
    Thanks! I don't think ProjectileManager can have a reference to DroneActions, can it? DroneActions is the script that's on the prefab. There are many copies of it and those are instantiated at runtime so how would ProjectileManager know which copy of DroneActions it's dealing with? This is why I wanted to have the singleton ProjectManager listening for the event from all the drone objects.

    Wouldn't each drone need a reference to ProjectileManager instead (which I was trying to avoid with the C# event)?
     
  4. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    335
    So if ProjectileManager is a Singleton your Drone Actions can call ProjectileManager.instance.DroneAquiredTarget

    No event required. If you want to use events and not expose a method like DroneAquiredTarget on your singleton, you can also call a method on your Singleton where you pass your drone in to register it with the manager. The manger can then add itself as a listener to the event on the drone.
     
    winterfive likes this.
  5. winterfive

    winterfive

    Joined:
    May 21, 2018
    Posts:
    31
    Wow, I would have never thought of that! Thanks! Learn something new everyday.
     
    MartinTilo likes this.
unityunity