Search Unity

'System.Array' does not contain a definition for 'Lenght'

Discussion in 'Scripting' started by TwentySicks, Oct 3, 2013.

  1. TwentySicks

    TwentySicks

    Joined:
    Sep 6, 2012
    Posts:
    51
    Hey guys, I'm trying to modify this script

    http://www.youtube.com/watch?v=6WBeH7hsfic

    But i cant find the lenght of an array in a dictionary, can any of you guys take a look at it and maybe you can give me some fixes to this as im completely stumped xD

    The error is from Line 33!

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6.  
    7. public class Animation : MonoBehaviour {
    8.    
    9.     public Dictionary<string, Texture[]> _animations = new Dictionary<string, Texture[]>();
    10.  
    11.  
    12.     public float FPS;
    13.     private float secondsToWait;
    14.     public bool Loop;
    15.    
    16.     private int currentFrame;
    17.    
    18.     public string currentAnimation = "walk";
    19.    
    20.     // Use this for initialization
    21.     void Start ()
    22.     {
    23.         GetAnimation(4,"walk");
    24.        
    25.         currentFrame = 0;
    26.         secondsToWait = 1/FPS;
    27.         StartCoroutine(Animate());
    28.     }
    29.    
    30.     IEnumerator Animate()
    31.     {
    32.         bool stop = false;
    33.        
    34.         if(currentFrame >= _animations[currentAnimation].Lenght)
    35.         {
    36.             if(Loop == false)
    37.                 stop = true;
    38.             else
    39.                 currentFrame = 0;
    40.         }
    41.        
    42.         yield return new WaitForSeconds(secondsToWait);
    43.        
    44.         renderer.material.mainTexture = _animations[currentAnimation][currentFrame];
    45.         currentFrame++;
    46.        
    47.         if(stop == false)
    48.             StartCoroutine(Animate());
    49.     }
    50.    
    51.     void GetAnimation(int frames, string animationName)
    52.     {
    53.         Texture[] sprites = new Texture[frames];
    54.        
    55.         for (int i = 0; i < frames; i++)
    56.         {
    57.             sprites[i] = Resources.Load(this.name + "frame" + i) as Texture;
    58.         }
    59.        
    60.         _animations.Add(animationName,sprites);
    61.     }
    62. }
    63.  
     
    Last edited: Oct 3, 2013
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
  3. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    Use Count instead of Length
     
  4. TwentySicks

    TwentySicks

    Joined:
    Sep 6, 2012
    Posts:
    51
    English is not my native language, and I'm having a hard time understanding the text.

    As far as I understand, it's simply not possible to do what im trying to do?

    So I tried using a List<> instead of an array, and it made the errors go away, but when I run the game, nothing happens. It should be animating on a plane, but nothing happens :s

    Code using a List<>

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6.  
    7. public class AnimationScript : MonoBehaviour {
    8.    
    9.     public Dictionary<string, List<Texture>> _animations = new Dictionary<string, List<Texture>>();
    10.  
    11.  
    12.     public float FPS;
    13.     private float secondsToWait;
    14.     public bool Loop;
    15.    
    16.     private int currentFrame;
    17.    
    18.     public string currentAnimation = "walk";
    19.    
    20.     // Use this for initialization
    21.     void Start ()
    22.     {
    23.         GetAnimation(4,"walk");
    24.        
    25.         currentFrame = 0;
    26.         secondsToWait = 1/FPS;
    27.         StartCoroutine(Animate());
    28.     }
    29.    
    30.     IEnumerator Animate()
    31.     {
    32.         bool stop = false;
    33.        
    34.         if(currentFrame >= _animations[currentAnimation].Count)
    35.         {
    36.             if(Loop == false)
    37.                 stop = true;
    38.             else
    39.                 currentFrame = 0;
    40.         }
    41.        
    42.         yield return new WaitForSeconds(secondsToWait);
    43.        
    44.         renderer.material.mainTexture = _animations[currentAnimation][currentFrame];
    45.         currentFrame++;
    46.        
    47.         if(stop == false)
    48.             StartCoroutine(Animate());
    49.     }
    50.    
    51.     void GetAnimation(int frames, string animationName)
    52.     {
    53.         List<Texture> sprites = new List<Texture>(frames);
    54.        
    55.         for (int i = 0; i < frames; i++)
    56.         {
    57.             sprites[i] = Resources.Load(this.name + "frame" + i) as Texture;
    58.         }
    59.        
    60.         _animations.Add(animationName,sprites);
    61.     }
    62. }
    63.  
     
  5. TwentySicks

    TwentySicks

    Joined:
    Sep 6, 2012
    Posts:
    51
    Problem fixed guys, thanks for the answers!

    It didnt work because i had made a typo in the gameobjects name in the editor xD

    Silly me!
     
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I think you couldn't get the length of the array on the first try, not this one, is that you misspelled "length" in line 33. You can store an array in the dictionary and get the length, like you were doing, I'm pretty sure.
     
    Last edited: Oct 4, 2013
  7. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    if(currentFrame >= _animations[currentAnimation].Lenght)
    its LENGTH
     
    thelunarlfower likes this.