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 MP0732 · Jun 21, 2012 at 08:24 PM · addanimation clip

adding a clip to a animation component via script

Is it possible to do this? I get an error message telling me that "Property UnityEngine.component.animation is Read-Only". Does this mean that you cannot alter the animation component's properties via scripting?

Here's my script so far:

 @script RequireComponent(Animation)
   
 
 function Start () {
 
 animation.animation = "EnemyAdvanceLeft";
 }
 
 function Update () {

}

My gut is telling me that Unity probably doesn't know where to find the animation called "EnemyAdvanceLeft". It's just that the error message I received does not indicate that's the primary issue.

Thanks.

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
Best Answer

Answer by MP0732 · Jun 25, 2012 at 12:20 PM

In the end, I ended up assigning both possible scripts to each prefab, then simply assigning the one to be played via script. This means that each instance of the prefab carries an animation it does not need, but I found this to be simpler than trying to assign it during run-time, as I am still getting error messages when I try to do this.

Thanks again Mike for your help.

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 whydoidoit · Jun 25, 2012 at 12:41 PM 0
Share

Glad you found a solution :)

avatar image
1

Answer by whydoidoit · Jun 21, 2012 at 08:25 PM

You need to add the actual clip - with animation.AddClip().

Here's my C# script for adding all of the animations in a folder:

 using UnityEngine;
 using System.Collections;
 using System.Linq;
 
 [AddComponentMenu("System/Load Animations")]
 public class LoadAnimations : MonoBehaviour {
 
     public new string name;
     
     void Awake()
     {
         var clips = Resources.LoadAll("Animations/" + name, typeof(AnimationClip)).Cast<AnimationClip>();
         foreach (var c in clips)
         {
             animation.AddClip(c, c.name.Contains("@") ? c.name.Substring(c.name.LastIndexOf("@") + 1) : c.name);
         }
 
         foreach (var a in animation.Cast<AnimationState>())
         {
             a.enabled = true;
         }
     }
     
 }
Comment
Add comment · Show 4 · 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 MP0732 · Jun 21, 2012 at 09:06 PM 0
Share

O$$anonymous$$, thanks.

avatar image MP0732 · Jun 21, 2012 at 09:27 PM 0
Share

Well, I've looked around the web to try to figure out your script, and I'm sorry I just can't make sense of it.

I guess I don't understand why I can't just tell Unity where to find the animation clip I want to load and go from there.

avatar image whydoidoit · Jun 21, 2012 at 10:16 PM 0
Share

Sure so you can add the clip if it is stored in a variable too, that script is loading them from the resources folder. Using a variable it would look like this:

   var myClip : AnimationClip;

Or maybe

   var myClips : AnimationClip[];

Then you would add them using:

   animation.AddClip(myClip);

or: for(var a : AnimationClip in myClips) { animation.AddClip(a); }

avatar image whydoidoit · Jun 21, 2012 at 10:17 PM 0
Share

Then you would just drag them on to the object. Or you can name them nameOf$$anonymous$$y$$anonymous$$odel@nameOfAnimation and there's some settings to store the animations in the root model.

avatar image
0

Answer by MP0732 · Jun 22, 2012 at 05:21 PM

OK, thanks. Well, I loaded the C# script via the Immediate Window in MonoDevelop.

Then I tried to add the animation using the following script which I attached via code to the game object I had instantiated. It failed, and I got the following error: "No appropriate version of 'UnityEngine.Animation.AddClip' for the argument list '(UnityEngine.AnimationClip)' was found."

Here's my script:

 @script RequireComponent(Animation)
 
 function Start () {
 var enemyAdvanceLeft : AnimationClip;
 animation.AddClip(enemyAdvanceLeft);
 
 }
 
 function Update () {
 
 }

Any idea what I'm doing wrong? Thanks again.

Comment
Add comment · Show 7 · 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 whydoidoit · Jun 22, 2012 at 05:22 PM 0
Share

You need to actually get the animation clip itself from somewhere - what you have done there is define a variable with nothing in it. However, I've also made a mistake :( You have to do animation.AddClip(clip, name)

avatar image whydoidoit · Jun 22, 2012 at 05:24 PM 0
Share

So you need to make enemyAdvanceLeft a script level variable and assign it in the inspector.@script RequireComponent(Animation)

 var enemyAdvanceLeft : AnimationClip;

 function Start () {
     animation.AddClip(enemyAdvanceLeft,"advanceLeft");

 }

avatar image MP0732 · Jun 22, 2012 at 05:28 PM 1
Share

O$$anonymous$$, but that's the problem. I need to assign the animation during game time, i.e. via code. I can't assign it in the Inspector because the game object to which it is supposed to be attached is instantiated while the game is running.

Is that possible?

avatar image MP0732 · Jun 22, 2012 at 05:29 PM 0
Share

Also, I guess I thought that I ran your C# script above in order to make all of the animations I had created accessible via scripting. But maybe I was wrong about its purpose?

avatar image whydoidoit · Jun 22, 2012 at 05:33 PM 0
Share

So you are going to need to either:

  1. Load it from the resources folder doing this:

      enemyAdvanceLeft = Resources.Load("Path/To/Your/Animation", typeof(AnimationClip)) as AnimationClip;
    
    
  2. Create a special holder object for your animations.

    • Create an empty game object

    • attach a singleton script to it that has an array of animations clips that you assign in the inspector
      • script has a static member initialized in Awake()

      • It's set to don't destroy on load

          var clips : AnimationClip[];
            static var Instance : YourScriptName;
            
            function Awake() {
                   Instance = this;
                  DontDestroyOnLoad(gameObject);
            }
        
        

The from elsewhere you can access the clips using

 YourScriptName.clips[0]; //etc
     
Show more comments
avatar image
0

Answer by nonehill · Apr 09, 2015 at 06:16 PM

Check this out awesome example https://github.com/nonehill/DynamicallyLoadingAnimation

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 phreakhead · Sep 10, 2017 at 03:21 AM

If you don't want to mark your animations as legacy, you can use the new Playables API to play non-legacy AnimationClips:

 private List<PlayableGraph> graphs = new List<PlayableGraph> ();
 
 // Just call this function when you want to play an AnimationClip on a specific GameObject.
 // from https://docs.unity3d.com/Manual/Playables-Examples.html
 private PlayableGraph playAnim(AnimationClip clip, GameObject obj) {
     PlayableGraph playableGraph;
 
     AnimationPlayableUtilities.PlayClip(obj.AddComponent<Animator>(), clip, out playableGraph);
 
     // save all graphs we create and destroy them at the end of our scene.
     // you might need to optimize this if you make a lot of animations.
     graphs.Add (playableGraph);
 
     return playableGraph;
 }
 
 void OnDisable() {
     foreach (var g in graphs) {
         g.Destroy();
     }
     graphs.Clear ();
 }
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

7 People are following this question.

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

Related Questions

adding and removing objects (cameras) from other scenes than the one currently being played in 0 Answers

How to create a points between two other points 0 Answers

weird result when substracting/adding; 1 Answer

Transform[] syntax error 2 Answers

Add values to Int[] (SetTriangles) 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