- Home /
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 :)
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;
}
}
}
}
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.
Answer by eatsomechicken · Jun 01, 2018 at 11:21 PM
um what language is this ? i cant tell the difference in languages
Your answer
Follow this Question
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