Question by
impurekind · Sep 21, 2018 at 08:33 PM ·
animatorplayenterenable and disable script
Anim showing Invalid Layer Index '-1' error when inside OnEnable() but not Update()?
I using the following code:
Animator m_Animator;
float timer;
private void OnEnable()
{
//Fetch the Animator from your GameObject
m_Animator = GetComponent<Animator>();
if (XRSettings.enabled) // VR mode
if (WeaponSelectionController.weaponNumberRightHand == 2) // Shotgun
{
m_Animator.Play("ShotgunFireVR", -1, 0f);
}
}
timer = 0;
}
private void Update()
{
timer += 1;
if (XRSettings.enabled) // VR mode
{
if (OVRInput.GetDown(OVRInput.Button.SecondaryIndexTrigger)) // Button just pressed
{
if (WeaponSelectionController.weaponNumberRightHand == 2) // Shotgun
{
if (timer >= WeaponSelectionController.fireCoolDownTimeRightHand)
{
m_Animator.Play("ShotgunFireVR", -1, 0f);
timer = 0;
}
}
}
else if (OVRInput.Get(OVRInput.Button.SecondaryIndexTrigger)) // Button is held
{
if (timer >= WeaponSelectionController.fireCoolDownTimeRightHand)
{
if (WeaponSelectionController.weaponNumberRightHand == 2) // Shotgun
{
m_Animator.Play("ShotgunFireVR", -1, 0f);
timer = 0;
}
}
}
}
The error appears in the Unity console if I put the m_Animator.Play stuff in the onEnable() function and cycle through my weapons, but not when it's in the Update() function and I similarly cycle through my weapons. So I know the anim code technically works, but it keeps showing as an error if the very same anim stuff is put in the OnEnable() function.
Why? What's different between putting the code in Update() vs OnEnable()? And how do I do the anim stuff in OnEnable() without it coming up with the error?
Comment