0

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
halfer
  • 18,701
  • 13
  • 79
  • 158
diem_L
  • 371
  • 3
  • 18
  • So what variable is it claiming is null? – BugFinder Feb 06 '20 at 08:16
  • @BugFinder see my endings – diem_L Feb 06 '20 at 08:25
  • Your code is incredibly confusing. Are you sure your step objects are found? and that they have animations on? It looks like thats a place to start – BugFinder Feb 06 '20 at 08:32
  • yes everyone works fine, I just try to stop all the animations with a click on `Next Level` .. with a click on this button the method `nextLevel()` should start – diem_L Feb 06 '20 at 08:36
  • Now you're saying it works? that doesnt sound right – BugFinder Feb 06 '20 at 08:48
  • I said that my I am not able to stop all my animations with the Button Next Level. If Level 01 starts, all the animations starts, but they don't stop I I click on Next Level, because I get a NullReferenceExpection – diem_L Feb 06 '20 at 08:50
  • I dont think the variables are being set like you think they are – BugFinder Feb 06 '20 at 09:00

2 Answers2

2
i == Array_Lists.animStrings.Length

and not

i < Array_Lists.animStrings.Length

I suspect you never enter the for cycle.

Andrea
  • 5,756
  • 1
  • 28
  • 51
2

Your arrays are static, so they are created before Awake.

Your animAnimation is filled with nulls, and it doesn't change when you put components to Start_Script.AnimationStep001 and others. You need to fill animAnimation with components after Awake. And your scripts are interlinked, this is not very good. Consider creating and populating animAnimation from Start_Script.

UPD: add this at the end of your Start_Script.Awake:

Array_Lists.animAnimation = new [] {
    AnimationStep001,
    AnimationStep011,
    AnimationStep012
};
Hermesis
  • 156
  • 7