Invalid Layer Index '-1' ???

As you can see there is a the main problem in the title but there is more than one. These happen everytime I try to run a animation by buttons.
Errors:

Please help, I cannot seem to find any answers :frowning:

My coding with said issues:
(It is a javascript)

#pragma strict

internal var animator : Animator;

var moving_yet = false;

public var Arm1_clip : AnimationClip;
public var Leg1_clip : AnimationClip;
public var Leg2_clip : AnimationClip;

function Start () {
    //animator = GetComponent(Animator);
    animator = GetComponent(Animator);
}

function Update () {
   
    //Pressing keys to start animation -----------------------------------------------------------------------
    //Pressing w --------------------------------------------------------
    if (Input.GetKeyDown("w"))
    {
        if (moving_yet == false)
        {
            moving_yet = true;
        }
    }
   
    if (Input.GetKeyUp("w"))
    {
        if (moving_yet == true)
        {
            moving_yet = false;
        }
    }
    //--------------------------------------------------------------------
   
    //Pressing S ---------------------------------------------------------
    if (Input.GetKeyDown("s"))
    {
        if (moving_yet == false)
        {
            moving_yet = true;
        }
    }
   
    if (Input.GetKeyUp("s"))
    {
        if (moving_yet == true)
        {
            moving_yet = false;
        }
    }
    //--------------------------------------------------------------------
   
    //Using up arrow------------------------------------------------------
        if (Input.GetKeyDown(KeyCode.UpArrow))
    {
        if (moving_yet == false)
        {
            moving_yet = true;
        }
    }
    //--------------------------------------------------------------------
   
    //Using down arrow----------------------------------------------------
    if (Input.GetKeyUp(KeyCode.DownArrow))
    {
        if (moving_yet == true)
        {
            moving_yet = false;
        }
    }
    //--------------------------------------------------------------------
    //----------------------------------------------------------------------------------------------------------
   
   
   
    //Beginning animation --------------------------------------------------------------------------------------
    if (moving_yet == true)
    {
     animator.Play("Arm1_walking");
    animator.Play("Leg1_walking");
    animator.Play("Leg2_walking");
    
    }
    //----------------------------------------------------------------------------------------------------------
   
    //Stopping animation----------------------------------------------------------------------------------------
    if (moving_yet == false)
    {
        animator.Stop();
    }
   
}
1 Like

My first guess would be that you have not named your states the same way in the animator as you have in your code…?

5 Likes

How would one do this? (Sorry for such a long time for me to respond, was at school when I got the notification)

in the animator, is the state named “Arm1_walking” and so on (do the 3 names match)?

2 Likes

Yes, all of them are
3029772--226573--Screenshot (168).jpg 3029772--226574--Screenshot (169).jpg 3029772--226574--Screenshot (169).jpg 3029772--226573--Screenshot (168).jpg

Okay, sadly my experience is not very good. When you open , for example: Arm1_walking. You should see a different window . I think that is where the “names” are, inside there; those are the states. Inside the ‘Player’ controller, do you have states for these animations? I see some other warnings & an error in your screen shots. Maybe look up a quick tutorial on a recent version of Unity and how it works?
Sorry I couldn’t be more help.

Check this page if you already haven’t.

err, I thought that was the OP sending me a message. Those pages are what I’d look over if I was doing animation currently. :slight_smile: (I only have 3 simple animations, and I did my best to convey my little bit of knowledge in this thread).

I went and redone some of the script and rearranged stuff, now it looks like this

#pragma strict
internal var animator : Animator;
var moving_yet = false;



function Start () {

}
function Update () {
  
    //Pressing keys to start animation -----------------------------------------------------------------------
    //Pressing w --------------------------------------------------------
    if (Input.GetKeyDown("w"))
    {
        if (moving_yet == false)
        {
            moving_yet = true;
        }
    }
  
    if (Input.GetKeyUp("w"))
    {
        if (moving_yet == true)
        {
            moving_yet = false;
        }
    }
    //--------------------------------------------------------------------
  
    //Pressing S ---------------------------------------------------------
    if (Input.GetKeyDown("s"))
    {
        if (moving_yet == false)
        {
            moving_yet = true;
        }
    }
  
    if (Input.GetKeyUp("s"))
    {
        if (moving_yet == true)
        {
            moving_yet = false;
        }
    }
    //--------------------------------------------------------------------
  
    //Using up arrow------------------------------------------------------
        if (Input.GetKeyDown(KeyCode.UpArrow))
    {
        if (moving_yet == false)
        {
            moving_yet = true;
        }
    }
    //--------------------------------------------------------------------
  
    //Using down arrow----------------------------------------------------
    if (Input.GetKeyUp(KeyCode.DownArrow))
    {
        if (moving_yet == true)
        {
            moving_yet = false;
        }
    }
    //--------------------------------------------------------------------
    //----------------------------------------------------------------------------------------------------------
  
  
  
    //Beginning animation --------------------------------------------------------------------------------------
    if (moving_yet == true)
    {
        GetComponent.<Animation>().Play("Arm1_walking");
        GetComponent.<Animation>().Play("Leg1_walking");
        GetComponent.<Animation>().Play("Leg2_walking");
    }
    //----------------------------------------------------------------------------------------------------------
  
    //Stopping animation----------------------------------------------------------------------------------------
    if (moving_yet == false)
    {
        animator.Stop();
    }
  
}

I now have two new errors which I need help fixing

Where would I go to to fix my issue?

You never assign your animator.
Friendly suggestion: Try c# , as many more people use it , it’s easier to get (and give) advice.
Not sure about the other error.

Found out how but these errors show no real error. They still pop up even though I have fixed everything.
Here is the new script:

#pragma strict
internal var animator : Animator;
var moving_yet = false;


//Animations vars-------------------------------------------------------------------------------------------
var arm1_animations = false;//Tells if it is the animation
var leg1_animations = false;//Same as above
var leg2_animations = false;//same
//----------------------------------------------------------------------------------------------------------
function Start () {

}
function Update () {
  
    //Pressing keys to start animation -----------------------------------------------------------------------
    //Pressing w --------------------------------------------------------
    if (Input.GetKeyDown("w"))
    {
        if (moving_yet == false)
        {
            moving_yet = true;
        }
    }
  
    if (Input.GetKeyUp("w"))
    {
        if (moving_yet == true)
        {
            moving_yet = false;
        }
    }
    //--------------------------------------------------------------------
  
    //Pressing S ---------------------------------------------------------
    if (Input.GetKeyDown("s"))
    {
        if (moving_yet == false)
        {
            moving_yet = true;
        }
    }
  
    if (Input.GetKeyUp("s"))
    {
        if (moving_yet == true)
        {
            moving_yet = false;
        }
    }
    //--------------------------------------------------------------------
  
    //Using up arrow------------------------------------------------------
        if (Input.GetKeyDown(KeyCode.UpArrow))
    {
        if (moving_yet == false)
        {
            moving_yet = true;
        }
    }

    if (Input.GetKeyUp(KeyCode.UpArrow))
    {
        if (moving_yet == true)
        {
            moving_yet = false;
        }
    }
    //--------------------------------------------------------------------
   
   
    //Using down arrow------------------------------------------------------
     if (Input.GetKeyDown(KeyCode.DownArrow))
     {
         if (moving_yet == false)
         {
             moving_yet = true;
         }
     }
    
     if (Input.GetKeyUp(KeyCode.DownArrow))
     {
         if (moving_yet == true)
         {
             moving_yet = false;
         }
     }
   
    //----------------------------------------------------------------------------------------------------------
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
    //Beginning animation --------------------------------------------------------------------------------------
    if (moving_yet == true && arm1_animations == true)
    {
        GetComponent.<Animation>().Play("Arm1_walking");
        //GetComponent.<Animation>().Play("Leg1_walking");
        //GetComponent.<Animation>().Play("Leg2_walking");
    }
    //----------------------------------------------------------------------------------------------------------
   
        //Beginning animation --------------------------------------------------------------------------------------
    if (moving_yet == true && leg1_animations == true)
    {
        GetComponent.<Animation>().Play("Leg1_walking");
    }
    //----------------------------------------------------------------------------------------------------------
   
            //Beginning animation --------------------------------------------------------------------------------------
    if (moving_yet == true && leg2_animations == true)
    {
        GetComponent.<Animation>().Play("Leg2_walking");
    }
    //----------------------------------------------------------------------------------------------------------
   
  
    //Stopping animation----------------------------------------------------------------------------------------
    if (moving_yet == false)
    {
        animator.Stop();
    }
  
}

I also had to remove a few animations and make sure each one only has one in animations section.

The Animator on line 2 doesn’t seem to be assigned anywhere that I can see, is to what I was referring.
You should read over the docs the other poster in this thread suggested. Maybe just to be familiar with/see some examples. Something might stand out to you, or you might try a new way. I think in the end you’re more likely to get what you want, at least.

It was used at line 134. I figured that out when I got the error and yes I tried changing the code to make it work without it but still won’t work so I will live with the errors instead of the non-playable parts

Fair enough. Maybe you’ll look into the docs/tutorials later to get it all working when you’re ready. :slight_smile:

Thats for polishing up the game before we ship it off to the inter webs :slight_smile: Gotta make sure everything works before we make any polishing.

For sure. sometimes it’s good to move forward, build more and come back to broken piece… just to keep things moving. Anyways, good luck with your project/game :slight_smile:

I had the same problem, when i deleted the animations and created them again with same name, the problem solved now . I just written these for future viewers…

1 Like

same question.you are right.Thank you!

Yes exactly, animations need to be named as they are named in animation controller.
Thanks