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

Hey guys, I’m trying to modify this script

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!

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class Animation : MonoBehaviour {
	
	public Dictionary<string, Texture[]> _animations = new Dictionary<string, Texture[]>();


	public float FPS;
	private float secondsToWait;
	public bool Loop;
	
	private int currentFrame;
	
	public string currentAnimation = "walk";
	
	// Use this for initialization
	void Start () 
	{
		GetAnimation(4,"walk");
		
		currentFrame = 0;
		secondsToWait = 1/FPS;
		StartCoroutine(Animate());
	}
	
	IEnumerator Animate()
	{
		bool stop = false;
		
		if(currentFrame >= _animations[currentAnimation].Lenght)
		{
			if(Loop == false)
				stop = true;
			else
				currentFrame = 0;
		}
		
		yield return new WaitForSeconds(secondsToWait);
		
		renderer.material.mainTexture = _animations[currentAnimation][currentFrame];
		currentFrame++;
		
		if(stop == false)
			StartCoroutine(Animate());
	}
	
	void GetAnimation(int frames, string animationName)
	{
		Texture[] sprites = new Texture[frames];
		
		for (int i = 0; i < frames; i++)
		{
			sprites[i] = Resources.Load(this.name + "frame" + i) as Texture;
		}
		
		_animations.Add(animationName,sprites);
	}
}

Use Count instead of Length

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<>

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class AnimationScript : MonoBehaviour {
	
	public Dictionary<string, List<Texture>> _animations = new Dictionary<string, List<Texture>>();


	public float FPS;
	private float secondsToWait;
	public bool Loop;
	
	private int currentFrame;
	
	public string currentAnimation = "walk";
	
	// Use this for initialization
	void Start () 
	{
		GetAnimation(4,"walk");
		
		currentFrame = 0;
		secondsToWait = 1/FPS;
		StartCoroutine(Animate());
	}
	
	IEnumerator Animate()
	{
		bool stop = false;
		
		if(currentFrame >= _animations[currentAnimation].Count)
		{
			if(Loop == false)
				stop = true;
			else
				currentFrame = 0;
		}
		
		yield return new WaitForSeconds(secondsToWait);
		
		renderer.material.mainTexture = _animations[currentAnimation][currentFrame];
		currentFrame++;
		
		if(stop == false)
			StartCoroutine(Animate());
	}
	
	void GetAnimation(int frames, string animationName)
	{
		List<Texture> sprites = new List<Texture>(frames);
		
		for (int i = 0; i < frames; i++)
		{
			sprites[i] = Resources.Load(this.name + "frame" + i) as Texture;
		}
		
		_animations.Add(animationName,sprites);
	}
}

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!

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.

if(currentFrame >= _animations[currentAnimation].Lenght)
its LENGTH

1 Like