Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 AnimeKuNyo · Dec 10, 2012 at 12:29 PM · c#animationgameobjectprefabs

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?

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

5 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 6 · 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 AnimeKuNyo · Dec 10, 2012 at 01:29 PM 0
Share

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)

avatar image IT_Criminal · Dec 10, 2012 at 01:51 PM 0
Share

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

avatar image AnimeKuNyo · Dec 10, 2012 at 03:24 PM 0
Share

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.

avatar image Jake.OConnor · Dec 10, 2012 at 03:28 PM 0
Share

Anime$$anonymous$$uNyo, you can use AddComponent() in order to add an animation component to the newly instantiated GameObject.

avatar image AnimeKuNyo · Dec 10, 2012 at 03:53 PM 0
Share

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

Show more comments
avatar image
0

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)

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 Jake.OConnor · Dec 10, 2012 at 04:23 PM 0
Share

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.

avatar image
0

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 ...

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
avatar image
0

Answer by AnimeKuNyo · Dec 12, 2012 at 08:48 AM

But the animation is attach to the prefab.

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
avatar image
0

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.

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

11 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

Related Questions

Game object with animations 0 Answers

Instances of one prefab work wrong together,OnMouseOver works wrong with instances of prefabs 0 Answers

Question about attaching game objects to certain bones 1 Answer

Instantiated Bullet Lags Behind Animated Timeline Object 1 Answer

In Game Animation 2 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