Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Cannot apply indexting with [] to an expression of type "Animator"

Discussion in 'Scripting' started by Vanderley5974, Mar 7, 2020.

  1. Vanderley5974

    Vanderley5974

    Joined:
    Nov 27, 2016
    Posts:
    21
    Hi! I'm relatively new to using unity and i wanted to do an animation that plays for the player when he's walking, that being the head bobbing animation. I wanted to have the headbob keep playing when the player let's go ofthe key, but will only play until the end of the animation (AKA not loop). But i'm not sure how to do that, i tried this but it hasnt worked. instead i get "Cannot apply indexting with [] to an expression of type "Animator""

    i'm not sure what i'm doing wrong since i'm very new to C# and unity, so i've come here looking for help! Much appreciated!

    Error line = 22

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HeadBobbing : MonoBehaviour
    6. {
    7.     public Animator Anim;
    8.     void Start()
    9.     {
    10.         Anim = GetComponent<Animator>();
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         if (Input.GetKeyDown((KeyCode.W)))
    17.         {
    18.             Anim.Play("HeadBob");
    19.         }
    20.         else if (Input.GetKeyUp(KeyCode.W))
    21.         {
    22.             Anim["HeadBob"].wrapMode = WrapMode.Once;
    23.             if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Headbob"))
    24.             {
    25.                 Anim.Play("Headbob");
    26.             }
    27.         }
    28.     }
    29. }
    30.  
     
  2. DoggyBoggy

    DoggyBoggy

    Joined:
    Jan 19, 2018
    Posts:
    20
    So, i have no experience with the Animator class, but i looked for the docs on unity and apparently in their example they use a foreach loop, and i don't know why if you can use a foreach loop they wouldn't just make it index-able as well, but according to unity this should work...

    Code (CSharp):
    1. foreach (AnimationState state in Anim)
    2. {
    3.     state.wrapMode = state.name == "HeadBob" ? WrapMode.Once : state.wrapMode;
    4. }
     
  3. Vanderley5974

    Vanderley5974

    Joined:
    Nov 27, 2016
    Posts:
    21
    I'm getting a error that says "foreach statement cannot operate on variables of type 'Animator' because 'Animator' does not contain a public instance definition for 'GetEnumerator'
     
  4. DoggyBoggy

    DoggyBoggy

    Joined:
    Jan 19, 2018
    Posts:
    20
    Oh, my bad, i was looking at the docs for Animation, not Animator. Bad news, i now very little about this, good news, i managed to find this treasure scattered in the docs of Animator and it might be useful to you.

    Animator.GetCurrentAnimatorStateInfo()

    but as for setting parameters on an AnamationState in an animator, while indirect, you may be able to utilize these:

    Animator.SetTrigger()
    and
    Animator.ResetTrigger()
     
    Last edited: Mar 7, 2020
    Vanderley5974 likes this.
  5. Vanderley5974

    Vanderley5974

    Joined:
    Nov 27, 2016
    Posts:
    21
    Alrighty, thanks! I'm still COMPLETELY new to C# and unity, so i'll see what i can do with that! any help is a lot =)