- Home /
Problem to play animation in C#
I have a prefab of 3 components and one of these 3 components have an animation attached on it. In my code i have an array of GameObjects, and i fill it with clones of this prefab. And then i try to play the animation, i tried many solution but nothing worked. Here is what i tried :
objArrayGame[i,j].GetComponent("Gearing").animation.Play("RotateGearing"); objArrayGame[i,j].GetComponent("Gearing").animation.Play("RotateGearing"); objArrayGame[i,j].animation.Play("RotateGearing");
and i try to put this "AddClip(animation.clip,"RotateGearingDirect");" before each animation.Play.
How can i do?
Answer by IT_Criminal · Dec 10, 2012 at 12:43 PM
i see no reason for a getcomponent statement here .... just
GameObjectArray[123].animation.Play("clipName");
where 123 is the location of the gear gameobject
I got this error message: $$anonymous$$issingComponentException: There is no 'Animation' attached to the "Gearing(Clone)" game object, but a script is trying to access it. You probably need to add a Animation to the game object "Gearing(Clone)". Or your script needs to check if the component is attached before using it. UnityEngine.Animation.Play (System.String animation) (at C:/BuildAgent/work/842f9557127e852/Runtime/ExportGenerated/Editor/Animations.cs:411) Jeu.InitTableauJeu () (at Assets/Scripts/Jeu.cs:132) Jeu.Start () (at Assets/Scripts/Jeu.cs:36)
you need to add animation components to objects that you want to animate .... to do that just select the object that you want to add an animation component to in the hierarchy view ... then go to the menu Component->$$anonymous$$iscellaneous->Animation .... make sure it's "Animation" and not "Animator" ... then in the inspector view you can set the size of the animation list and add animations to your object
But in my case that didn't work because my gameObject is created in my script like that: objArrayGame[i,j] = Instantiate(tGearing.gameObject, vector, Quaternion.identity) as GameObject; tGearing is type tramform. And is not created before start the program.
Anime$$anonymous$$uNyo, you can use AddComponent() in order to add an animation component to the newly instantiated GameObject.
I tried that before but I got an error, which says me that the component doesn't exist. It's possible I put a wrong thing in the function parameters, i do that: objArrayGame [i,j].AddComponent("Gearing");
tGering is the prefab named Gearing who got 3 GameObject (bloc, stem, and gearing). gearing got the animation "RotateGearing" attach on him.
This is my script, I got the error message $$anonymous$$issingComponentException(description in my second post)in the second instruction in my case 2:
public class Game : $$anonymous$$onoBehaviour {
public Transform tWall;
public Transform tBloc;
public Transform tGearing;
private GameObject [,]objArrayGame = new GameObject[6,6];
private int [,]iArrayGame = new int[6,6] {{0,0,0,0,0,0}, {2,4,1,1,2,0}, {0,5,4,4,1,0}, {0,1,4,4,1,0}, {0,4,1,1,4,3}, {0,0,0,0,0,0}};
private Object obj;
private Vector3 vector = new Vector3(0.5f, 0.5f, 0.5f);
void Start () {
InitArrayGame();
}
void InitArrayGame ()
{
for (int i = 0; i< 6 ; ++i) {
for (int j = 0; j< 6 ; ++j) { vector.x = j+0.5f; vector.z = i+0.5f;
switch (iArrayGame [i,j]) {
case 0: objArrayGame [i,j] = Instantiate(tWall.gameObject,vector,Quaternion.identity) as GameObject; break;
case 1:
objArrayGame [i,j]= Instantiate(tBloc.gameObject,vector,Quaternion.identity) as GameObject; break;
case 2:
objArrayGame [i,j] = Instantiate(tEngrenage.gameObject,vector,Quaternion.identity) as GameObject;
objArrayGame [i,j].animation.Play("RotateGearing"); break;
case 3:
objArrayGame [i,j] =Instantiate(tGearing.gameObject,vector,Quaternion.identity) as GameObject; break;
case 4:
objArrayGame [i,j] = Instantiate(tGearing.gameObject,vector,Quaternion.identity) as GameObject; break;
default: objArrayGame [i,j] = null; break; } } } } }
Answer by AnimeKuNyo · Dec 10, 2012 at 04:10 PM
I got this error message: MissingComponentException: There is no 'Animation' attached to the "Gearing(Clone)" game object, but a script is trying to access it. You probably need to add a Animation to the game object "Gearing(Clone)". Or your script needs to check if the component is attached before using it. UnityEngine.Animation.Play (System.String animation) (at C:/BuildAgent/work/842f9557127e852/Runtime/ExportGenerated/Editor/Animations.cs:411) Jeu.InitTableauJeu () (at Assets/Scripts/Jeu.cs:132) Jeu.Start () (at Assets/Scripts/Jeu.cs:36)
That's what I've been saying. If you're going to call an animation on something, it must have an Animation component and an AnimationClip in order to play. Trace through your code and find out where you're calling ".animation.Play(", then make sure that ".animation" isn't null.
Answer by IT_Criminal · Dec 11, 2012 at 12:53 PM
just add an animation component to the prefab that you are instantiating and your problems will be gone .... and you DON'T need getcomponent ...
Answer by AnimeKuNyo · Dec 14, 2012 at 01:31 PM
But the animation is on the prefab. I got a prefab called Gearing with 3 component -Cube -stem -gearing (how have the animation)
Is for that i try to get the component gearing of my prefab Gearing And if i do that objArrayGame[i,j].animation.Play("RotateGearing");
MissingComponentException: There is no 'Animation' attached to the "Gearing(Clone)" game object, but a script is trying to access it. You probably need to add a Animation to the game object "Gearing(Clone)". Or your script needs to check if the component is attached before using it.