Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unitygamer9 · Apr 02, 2017 at 07:17 PM · scripting problemscripting beginneranimationclipupdate function

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");
                     }
                }
     }
 }

 `

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Commoble · Apr 02, 2017 at 07:17 PM 0
Share

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.

avatar image unitygamer9 Commoble · Apr 03, 2017 at 05:47 PM 0
Share

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

avatar image unitygamer9 · Apr 03, 2017 at 04:03 PM 0
Share

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

avatar image adi_tya · Apr 03, 2017 at 05:32 PM 0
Share

@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])"

avatar image unitygamer9 adi_tya · Apr 03, 2017 at 05:47 PM 0
Share

@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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges