В моем проекте Unity у меня есть несколько уровней и несколько шагов. Каждый шаг и уровень имеют свои собственные анимации. Нажав кнопку, я хочу остановить все анимации, поэтому я попробовал это следующим образом. Для моего проекта у меня есть два сценария:
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
};
Но, к сожалению, я всегда получаю NullReferenceException: Object reference not set to an instance of an object
для Array_Lists.animAnimation[i].Stop(Array_Lists.animStrings[i]);
Таким образом, мне нужно остановить все анимации, когда я нажимаю кнопку «Следующий уровень».
Редактировать
После моего первого NullReferenceException:
я добавил следующее к моему public void nextLevel()
методу:
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;
}
Это был мой вывод:
Array Animations = null
Array anim Name = animStep1
Array Animations = null
Array anim Name = animStep1.1
Array Animations = null
Array anim Name = animStep1.2