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.
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.
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?
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.