- Home /
Solved it by putting animation manager script on each UI panel that was to be animated.
Invalid Layer Index?
OK this is weird, I have two animations that I start from a C# script, the animations are fine and they work OK but one of them will not play in my UI interface set up? I get the following error:
Invalid Layer Index UnityEngine.Animator:Play(String) ScannerAdjustmanager:StartScannerLights() (at Assets/ScannerAdjustmanager.cs:42) UnityEngine.EventSystems.EventSystem:Update()
In my inspector I have the Animator Unchecked and my C# script is supposed to activate them when I press a UI button, but only one of them works, and I get that error message above? I'll post my script just incase anyone can see something I'm missing.
using UnityEngine;
using System.Collections;
public class ScannerAdjustmanager : MonoBehaviour {
//Reference to Animations
public GameObject ScannerLightAni;
public GameObject BlueLEDsAni;
//Audio Clip References
public AudioClip Scanner;
//animator reference
private Animator anim;
void Start () {
//unpause the game on start
//Time.timeScale = 1;
//get the animator component
anim = ScannerLightAni.GetComponent<Animator>();
anim = BlueLEDsAni.GetComponent<Animator>();
//disable it on start to stop it from playing the default animation
//anim.enabled = false;
}
public void GoAbort(){
StartCoroutine(LoadAfterDelay("ThreeD_Overview"));
}
IEnumerator LoadAfterDelay(string levelName){
yield return new WaitForSeconds(0.5f); // wait 1 seconds
Application.LoadLevel(levelName);
}
//Start Scanner Animations
public void StartScannerLights(){
//StartCoroutine(AutoDoorOutR());
anim.enabled = true;
anim.Play("ScannerAdjustAnimation");
}
//Start LEDs Animations
public void StartLEDLights(){
//StartCoroutine(AutoDoorOutR());
anim.enabled = true;
anim.Play("ScannerBlueLEDsAni");
//Audio Play
audio.clip = Scanner;
audio.loop = true;
audio.Play();
}
}
Answer by meat5000 · Feb 16, 2015 at 01:16 AM
It wants more information. Layer and Normalised Time.
http://docs.unity3d.com/ScriptReference/Animator.Play.html
It appears you are trying to use it the Legacy way.
Well, I have no clue what that meant, but I did discover that if I made a separate script for each animation and called them from my UI buttons and that solved the problem.
It says Invalid Layer Index because according to the page I linked
animator.Play wants you to tell it what layer the state is in you are trying to play.
But I almost told you lies, it does support a string.
It was confusing me because they were all on the UI layer which would make sense to me, but for some reason it just did not like my calling it from the same script, soon as I made a separate script and dragged that onto the animation and then dragged the animation onto my UI button it worked.
Follow this Question
Related Questions
Animation not looping 1 Answer
Create animation transitions via script. 0 Answers
Mirroring animation clip without mirroring root motion node? 0 Answers
The animation does not play when I click 2 Answers
Wierd flickering animation 1 Answer