In my Unity project I have several levels and several steps. Each step and level have their own animations. With a button click I want to stop all animations, so I tried it the following way. For my project I have two scripts:
Start_Script.cs
//Steps for Level 01:
public static GameObject Step001, Step011, Step012;
//Each animation will play in Level 01:
public static Animation AnimationStep001, AnimationStep011, AnimationStep012;
int levelCounter;
void Awake()
{
levelCounter = 1;
Step001 = GameObject.Find("Step001");
Step011 = GameObject.Find("Step011");
Step012 = GameObject.Find("Step012");
AnimationStep001 = Step001.GetComponent<Animation>();
AnimationStep011 = Step011.GetComponent<Animation>();
AnimationStep012 = Step012.GetComponent<Animation>();
}
//Method is assigned to "Next Level" Button.
public void nextLevel()
{
switch(levelCounter)
{
case 1:
for(int i = 0; i == Array_Lists.animStrings.Length; i++)
{
Array_Lists.animAnimation[i].Stop(Array_Lists.animStrings[i]);
}
break;
}
levelCounter += 1;
}
Array_Lists.cs
// My Array for the animations name:
public static string[] animStrings = {"animStep1", "animStep1.1", "animStep1.2"};
// My Array for the animation objects:
public static Animation[] animAnimation = {
Start_Script.AnimationStep001,
Start_Script.AnimationStep011,
Start_Script.AnimationStep012
};
But unfortunately I always get a NullReferenceException: Object reference not set to an instance of an object
for Array_Lists.animAnimation[i].Stop(Array_Lists.animStrings[i]);
In summary, I need to stop all animations when I hit the "Next Level" Button.
Edit
After my first NullReferenceException:
I added a the following to my public void nextLevel()
method:
public void nextLevel()
{
switch(levelCounter)
{
case 1:
for(int i = 0; i == Array_Lists.animStrings.Length; i++)
{
Debug.Log("Array Animations = " + Array_Lists.animAnimation[i];
Debug.Log("Array anim Name = " + Array_Lists.animStrings[i];
Array_Lists.animAnimation[i].Stop(Array_Lists.animStrings[i]);
}
break;
}
levelCounter += 1;
}
This was my output:
Array Animations = null
Array anim Name = animStep1
Array Animations = null
Array anim Name = animStep1.1
Array Animations = null
Array anim Name = animStep1.2