- Home /
How to an animation clip based on characters(letters) in an array
Hello,
I would like to play a particular animation clip based on character present in an array. I have written the following code. But its not working. Please help me. Thanks in advance
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour {
Animator anim;
char[] test = {'A','B'};
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
for(int i = 0;i<2;i++)
{
// Play animation clip "A", if the character is A
if(test[i]=='A')
{
anim.Play("A");
}
// Play animation clip "B", if the character is B
if(test[i]=='B')
{
anim.Play("B");
}
}
}
}
`
This is what your script currently does:
On startup, you set the character array to {A, B}. Character 0 is A, and Character 1 is B.
Then, in each update, you look at each character in the array. If that character is A, you play animation A. If that character is B, you play animation B.
The problem that you have is that you're iterating over every character in the array, so you're calling all of your animations in every update.
You might be better off checking a single value rather than an array of values -- consider using an Enum ins$$anonymous$$d of a character.
@Commoble Thanks for your reply, but the problem is the characters in the array is not static, it is dynamically taken from user. For example if the user enters String "ABC". I need to play the animations A,B,C respectively. So, I thought looping over the array, but it doesn't work in update() function I think, so can you please suggest an alternative approach for it?
@Commoble Thanks for your reply, but the problem is the characters in the array is not static, it is dynamically taken from user. For example if the user enters String "ABC". I need to play the animations A,B,C respectively. So, I thought looping over the array, but it doesn't work in update() function I think, so can you please suggest an alternative approach for it?
@unitygamer9 Dude, why are you using if statement.If you have to play all the animations in the character array then just iterate through them all by giving a common command of "anim.Play(test[i])"
@adi_tya, Thanks for the reply, but that for loop is not working properly in the update() function. It only works for the last animation clip that occurs that is "B" in this case
Your answer
Follow this Question
Related Questions
GetComponent won't work 1 Answer
How can i upgrade the fire rate and damage in my upgrade menu 1 Answer
How to create a mesh with coordinates given from an Arduino? 1 Answer
How to make that the camera will not go through the walls? 1 Answer
How do I make it so only one player spawns when I respawn? 0 Answers