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. Dismiss Notice

Unity 5 animation name + slicing problem

Discussion in 'Scripting' started by kalamona, Feb 22, 2015.

  1. kalamona

    kalamona

    Joined:
    Aug 16, 2011
    Posts:
    727
    Hey,
    I googled a bit, and I don't know how to solve this. It is pretty urgent, and I'd like to avoid too much workarounds, but perhaps there is no other way. Theres a small script that has problems with unity 5.

    The script does the following:
    - It uses Legacy animation clips, no mechanim
    - animations can be drag&dropped into its animList
    - it plays the random animation, and also changes its speed randomly.

    var animList : AnimationClip[];
    var actualAnim:AnimationClip;
    var minSpeed:float=0.7;
    var maxSpeed:float=1.5;

    function Start ()
    {
    var rnd=Mathf.Round(Random.Range(0,animList.length));
    actualAnim = animList[rnd];
    animation.Play(actualAnim.name); //this is kinda okay
    animation[actualAnim.name].speed = Random.Range(minSpeed, maxSpeed); //here is the problem!!
    }​

    It worked well in unity 3 and 4. However upgrading to 5, the following error is here (last line):
    "BCE0048: Type 'UnityEngine.Component' does not support slicing."

    Now it might be a stupid idea, but I think that since you have to put the name of the animation that should be played between [ and ], but that also shows that something is an array, that somehow creates a problem. It is just a wild guess, sadly I am not a programmer. But I totally need to fix this, as a lot of my assets use this script.

    Additional information: if I comment out the problematic line (the last one), then Unity can run its automatic script updater. That changes a line:

    (variables, etc stay the same)

    animation.Play(actualAnim.name) becomes
    GetComponent.<Animation>().Play(actualAnim.name);

    (end)

    Please tell me how could I fix this script so that it works under unity 5, preferably so that already existing scripts in a scene and prefab don't lose their animList property :/ Thanks so much!
     
    Zaborius likes this.
  2. Zaborius

    Zaborius

    Joined:
    Mar 15, 2013
    Posts:
    9
    Just replace "animation[actualAnim.name]" with GetComponent.<Animation>()[actualAnim.name]
     
    SchChris and kalamona like this.
  3. DogF

    DogF

    Joined:
    Dec 31, 2013
    Posts:
    29
    Thankyou very mush. Problem dead now.
     
  4. Xaanyx

    Xaanyx

    Joined:
    Sep 25, 2016
    Posts:
    1
    Hello! I was wonder how I replace "animation[actualAnim.name]" with GetComponent.<Animation>()[actualAnim.name]?
     
  5. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    animation is a shortcut to an object (like transform). they took out most of these.
    To replace it you just actually take out "animation" and replace it with "GetComponent<Animation>()" (no dots)
    so
    Code (CSharp):
    1. animation[actualAnim.name]
    becomes:
    Code (CSharp):
    1. GetComponent<Animation>()[actualAnim.name]
     
    kalamona likes this.