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 DarkFlameMaster · Jun 01, 2018 at 11:18 PM · animationarraygetcomponentstringforeach

C# Getting a certain AnimationState using strings from all objects with specific tag

I have a ball maze game, and I am currently scripting on resetting all walls(I have an animation that resets walls, strings.EndsWith("rev")) that are being animated "open" referred as "down". But the problem is it would turn into bool as I get string.EndsWith("down") What I want is to get the statename string.

Here's the code:

 public class ResetScript : MonoBehaviour {
 
     private GameObject[] ani;
     private Animation animator;
     string[] animNames;
     string statenames;
     string anidown;         // it shoud be bool but I want it to be string
     string anirev;        // the same^ and then I would get error of "Cannot implicitly convert type 'bool' to 'string'
 
     // Use this for initialization
     void Start()
     {
         ani = GameObject.FindGameObjectsWithTag("AnimatedWalls");
 
         foreach (GameObject animation in ani)
         {
             animator = animation.GetComponent<Animation>();
 
             foreach (AnimationState state in animator)
             {
                 statenames = state.name;
                 animNames = new string[] { statenames };
                 anidown = Array.Exists(animNames, element => element.EndsWith("down"));
                 anirev = Array.Exists(animNames, element => element.EndsWith("rev"));
             }
         }
     }
 
      
     // Update is called once per frame
     void OnCollisionEnter(UnityEngine.Collision collisionInfo)
     {
 
          if (animator.IsPlaying(anidown.ToString()))
          {
             animator.Play(anirev.ToString());
 
          }
          
     }
 }
 

This is my last hope to solve this script

  • P.S:

  • >I didn't use Animator Component

  • >First time asking a question

  • >I'm a newbie :)

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by b1gry4n · Jun 01, 2018 at 11:43 PM

This will never work because you are trying to store each " animated wall" 's animator, but you only have 1 reference animator, which means you are just setting it over and over. Even if you have multiple "resetscripts" in your world, by calling GameObject.FindGameObjectsWithTag("AnimatedWalls"); you are getting EVERY object in your scene with the tag "AnimatedWalls". When your forloop is done it has only the last animated wall's animator/info/etc. You are overwriting references to the previous objects. I dont know if this will work...but hopefully it puts you on the right track. its basically a manager of the walls. you wouldnt need more than one in your scene. when you want to play a gameobjects animation you pass its gameobject into TriggerAGameobjectsAnimation(GameObject go)

         public class ResetScript : MonoBehaviour
         {
             public class AnimatedWallsInfo
             {
                 public GameObject wallObject;
                 public Animation anim;
                 public string[] animNames;
                 public string statenames;
                 public string anidown;
                 public string anirev;
 
                 public void PlayAnimation()
                 {
                     //you were doing "anidown.ToString()", but it is already a string.
                     if (anim.IsPlaying(anidown))
                     {
                         //you were doing "anirev.ToString()", but it is already a string.
                         anim.Play(anirev);
                     }
                 }
             }
             public List<AnimatedWallsInfo> walls;
 
             void Start()
             {
                 //setup our wall list
                 walls = new List<AnimatedWallsInfo>();
 
                 //finds every game object in the current scene with the tage "animatedwalls"
                 GameObject[] wallGroup = GameObject.FindGameObjectsWithTag("AnimatedWalls");
 
                 //for every gameobject we found
                 foreach (GameObject wall in wallGroup)
                 {
                     //create a new class
                     AnimatedWallsInfo animWallsInfo = new AnimatedWallsInfo();
                     //the gameobject reference for this specific wall 
                     animWallsInfo.wallObject = wall;
                     //the walls animator
                     animWallsInfo.anim = wall.GetComponent<Animation>();
 
 
                     //Im not really sure what youre trying to do in this area, but you would plug it in something like this
                     foreach (AnimationState state in animWallsInfo.anim)
                     {
                         animWallsInfo.statenames = state.name;
                         animWallsInfo.animNames = new string[] { animWallsInfo.statenames };
                         animWallsInfo.anidown = Array.Exists(animWallsInfo.animNames, element => element.EndsWith("down"));
                         animWallsInfo.anirev = Array.Exists(animWallsInfo.animNames, element => element.EndsWith("rev"));
                     }
                     //
 
                     walls.Add(animWallsInfo);
                 }
             }
 
             public void TriggerAGameobjectsAnimation(GameObject go)
             {
                 //for all the walls we stored
                 for (int i = 0; i < walls.Count; i++)
                 {
                     //if the reference gameobject is the one we want to play
                     if (walls[i].wallObject == go)
                     {
                         //trigger the animation
                         walls[i].PlayAnimation();
                         break;
                     }
                 }
             }
         }
Comment
Add comment · Show 1 · Share
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 DarkFlameMaster · Jun 02, 2018 at 07:33 AM 0
Share

In this area:

  foreach (AnimationState state in animWallsInfo.anim)
                          {
                              animWallsInfo.statenames = state.name;
                              animWallsInfo.animNames = new string[] { animWallsInfo.statenames };
                              animWallsInfo.anidown = Array.Exists(animWallsInfo.animNames, element => element.EndsWith("down"));
                              animWallsInfo.anirev = Array.Exists(animWallsInfo.animNames, element => element.EndsWith("rev"));
                          }
 

I have 8 AnimationStates in my project and uniformly named (e.g AniWall01down & AniWall01rev and so on) and they are being assigned in each wall object with corresponding number, AnimatedWalls1, 2, 3, & 4.

what I want is that the "anidown" will get the strings that ends with down and "anirev" will get strings that ends with rev.

Your code was very informative but the error still occurs. Cannot implicitly convert type 'bool' to 'string' That is because of the Arrays right? and PlayAnimation will really won't run because inside the () is bool not string.

avatar image
-1

Answer by eatsomechicken · Jun 01, 2018 at 11:21 PM

um what language is this ? i cant tell the difference in languages

Comment
Add comment · Share
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

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

237 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Check if specific object exist in list or array, Best Practices? 2 Answers

Definition problem 1 Answer

Change part of a string [Solved] 3 Answers

how to get game object by string? 0 Answers

[SOLVED] First array slot blocking second array slot 1 Answer


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