I'm missing something with adding a new Animation event

I have added two events to animation and getting a “has no receiver”, I bet it’s something simple lol.

I have added them to the sword animation.

the code that calls it.

 public class Weapon : MonoBehaviour
    {
        [SerializeField] UnityEvent onHit;
        public List<TPHitBox> tPHitBoxes;
        [SerializeField] GameObject uiContainer = null;
        public bool friendly = true;

        public void OnHit()
        {
            onHit.Invoke();

        }
        private void Start()
        {

            uiContainer.SetActive(false);

            if (tPHitBoxes.Count > 0)
            {
                // initialize hitBox properties
              foreach (TPHitBox hitBox in tPHitBoxes)
              {
                    hitBox.weaponObject = this;
              }
            }
            else
            {
                this.enabled = false;
            }
        }
        void ToggleBoxOn()
        {
            uiContainer.SetActive(true);
        }
        void ToggleBoxOff()
        {
            uiContainer.SetActive(false);
        }

    }

and the error

I’m trying to use the animations to active and reactive the object like the ShowUI.

In my experience, explicitly setting the destination object for an animation doesn’t work well, because the animation lives in the Assets folder, and the object lives in the scene (if you referenced the item in assets, it doesn’t exist in the scenes).

I’m assuming that the component with the ToggleBoxOn/Off methods is on your Player. If the animation is played through the Animator, it will automatically broadcast the message to the GameObject it’s on if there is no object set in the Event. Try leaving off the Weapon reference, and make sure that the component responsible for responding to ToggleBoxOn/Off is attached to the player.

Umm, the ToggleBox is on the weapon and the uiContainer responding to the method is a child object to the Weapon.
Screenshot (7)

In trying something else, I change the ToggleBox to be on the same fighting script with the Hit event and call to replacement methods in the Weapon but now I’m getting a new error.


fighting script:

        void ToggleBoxOn()
        {
            weapon.TBoxOn();
        }
        void ToggleBoxOff()
        {
            weapon.TBoxOff();
        }

weapon script:

    public class Weapon : MonoBehaviour
    {
        [SerializeField] UnityEvent onHit;
        public List<TPHitBox> tPHitBoxes;
        [SerializeField] GameObject uiContainer = null;
        public bool friendly = true;

        public void OnHit()
        {
            onHit.Invoke();

        }
        private void Start()
        {

            uiContainer.SetActive(false);

            if (tPHitBoxes.Count > 0)
            {
                // initialize hitBox properties
              foreach (TPHitBox hitBox in tPHitBoxes)
              {
                    hitBox.weaponObject = this;
              }
            }
            else
            {
                this.enabled = false;
            }
        }
        public void TBoxOn()
        {

          uiContainer.SetActive(true);

        }
        public void TBoxOff()
        {

          uiContainer.SetActive(false);
 
        }

    }

Guess I’m going to need to start from scratch, or instead of using the uiContainer just reach for the hitBox child to the weapon from the fighter script?

I’m a bit short on context, I think… I don’t know what it is that you’re trying to accomplish…

  • What is a HitBox?
  • What do you expect to happen with ToggleBox(on) and ToggleBox(off)

The hitBox is a component with a box collider (child to the weapon). The script has the OnTriggerEnter, so when the box collider colids with the game object tagged Enemy it causes damage. That part works, but I’m trying to use the Animation Event to toggle the box on and off so the enemy does not take damage from just walking into the weapon.

    public class TPHitBox : MonoBehaviour
    {
        Collider trigger;
        public Weapon weaponObject;
        TPFighting tpFighting;
        
        void Start()
        {
            trigger = GetComponent<Collider>();
            tpFighting = gameObject.GetComponentInParent<TPFighting>();

            if (trigger)
            {
               trigger.isTrigger = true;
              
            }      
        }

       public void OnTriggerEnter(Collider other)
       {
            Health target = other.transform.GetComponent<Health>();
            float damage = tpFighting.GetComponent<BaseStats>().GetStat(Stat.Strength);

            if (weaponObject.friendly == true && other.gameObject.tag == "Enemy")
            {
                print("Hit Enemy");
                target.TakingDamage(gameObject, damage);
                weaponObject.OnHit();

            }

            if (weaponObject.friendly == false && other.gameObject.tag == "Player")
            {
                print("Hit Player");

            }
        }
    }

I tried some other ways of toggling the box collider on and off or trigger enabled, but I think in doing that I’m messing up the TPHitBox script so I’m getting other errors of the destroyed object.

As these boxes are OnTriggerEnter, let’s rework the logic a bit, and forget about turning the triggerboxes on and off…

In Fighter.cs:

bool isTriggerActive=false;

void ToggleBoxOn()
{
     isTriggerActive=true;
}

void ToggleBoxOff()
{
     isTriggerActive=false;
}

public void ToggleBoxTriggered(Collider other)
{
    if(!isTriggerActive) return; //ignore bad hits.
    // do your damage stuff from here.
}

In your Trigger Boxes:

void OnTriggerEnter(Collider other)
{
     GetComponentInParent<Fighter>().ToggleBoxTriggered(Collider other);
}

Yeeeaaaa, thank you so much, got it to work! Now I can finally move on lol. Thank you again.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.