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 Trr1ppy · Jun 03, 2013 at 01:09 PM · c#

Animation Scripting c# help

Ive been trying to get this right for ages and ive decided to ask you guys. Does anyone have any idea why its not playing the animation when i assign this script to the parent of the model im trying to animate? using UnityEngine; using System.Collections;

public class EnemyAnimation : MonoBehaviour { Animation _animation;

 void Start()
 {
     _animation = GetComponentInChildren();
     
     string animationToPlay = "";
     switch (Random.Range(0,3))
     {
     default:
     case 0:
         animationToPlay = "Move1";
         break;
     case 1:
         animationToPlay = "Move2";
         break;
     case 2:
         animationToPlay = "Move3";
         break;
     }
     
     _animation[animationToPlay].wrapMode = WrapMode.Loop;
     
     _animation.Play(animationToPlay);
     _animation[animationToPlay].normalizedTime = Random.value;
 }

}

Unity debug screen was saying the line "_animation[animationToPlay].wrapMode = WrapMode.Loop;" was wrong. Sorry if i haven't displayed this correctly Just any help would be much appreciated!

Comment
Add comment · Show 5
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 TonyLi · Jun 03, 2013 at 01:47 PM 0
Share

What's the error message? Does it occur at compile time or run time?

(p.s. - When posting, please click the '101/010' button before pasting in code, so it gets formatted properly.)

avatar image SubatomicHero · Jun 03, 2013 at 01:52 PM 0
Share

Not sure if this helps, but shouldn't the default case in your switch statement be the LAST element of the switch?

Default is what the switch will drop to if the other cases aren't met. This code suggests that case 0 is your default. Sometimes logical errors can have knock on effects to other parts of your code.

avatar image Trr1ppy · Jun 03, 2013 at 01:56 PM 0
Share

Thanks for the tip :) During runtime. The message is "NullReferenceException: Object reference not set to an instance of an object EnemyAnimation.Start() (at Assets/PlayerAnimation.cs:23)

avatar image SubatomicHero · Jun 03, 2013 at 02:04 PM 0
Share

Try changing _animation[animToPlay].normalizedTime = 0.0f;

avatar image Jason991 · Sep 08, 2013 at 11:58 AM 0
Share

hey guys regarding the above correction i still cant get it to fix, i have the best overload method error do you guys know the reason for it?

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by SubatomicHero · Jun 03, 2013 at 01:56 PM

This line is causing the error:

 _animation[animationToPlay].wrapMode = WrapMode.Loop;

because you are using a string to access an element of an array, which should be accessed using an integer of somesort.

So in your Start() function add these bits:

 // at the top with _animation
 int animToPlay;
 
 // then in each case of your switch do this
 case 0:
     animationToPlay = "Move";
     animToPlay = 0; // 1, 2, 3 etc depending on the case
     break;
 
 // then at the bottom change the code to:
 
 _animation[animToPlay].wrapMode = WrapMode.Loop
 _animation.Play(animationToPlay);
 _animation[animToPlay].normalizedTime = Random.value;

Hope this helps.

Comment
Add comment · Show 2 · 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 Trr1ppy · Jun 03, 2013 at 02:05 PM 1
Share

Thanks for the help! animations work great now :)

avatar image SubatomicHero · Jun 03, 2013 at 02:10 PM 0
Share

Hey no problem, can you please mark my answer as correct. Thanks!

avatar image
0

Answer by wraithblade · Jul 23, 2015 at 10:55 AM

using UnityEngine; using System.Collections;

public class EnemyBodyScript : MonoBehaviour {

 void Start() {

     int
     _animation = GetComponentInChildren();

     string animationToPlay = "";
     switch (Random.Range (0, 3)) {
     default:
     case 0:
         animationToPlay = "Move1";
         animationToPlay = 0;
         break;
     case 1:
         animationToPlay = "Move2";
         animationToPlay = 1;
         break;
     case 2:
         animationToPlay = "Move3";
         animationToPlay = 2;
         break;
     case 3:
         animationToPlay = "Move4";
         animationToPlay = 3;
         break;
     case 4:
         animationToPlay = "Move5";
         animationToPlay = 4;
         break;
     case 5:
         animationToPlay = "Move6";
         animationToPlay = 5;
         break;
     case 6:
         animationToPlay = "Move7";
         animationToPlay = 6;
         break;
     }

     _animation[animationToPlay].wrapMode = WrapMode.Loop;
     _animation.Play(animationToPlay);
     _animation[animationToPlay].normalizedTime = Random.value;
 }

}

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

19 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

Related Questions

Multiple Cars not working 1 Answer

Trigger GUI with a key when colliding (C#)? 1 Answer

NullReferenceException problem 2 Answers

Pause Game When Function Is Enabled? 3 Answers

Player lives script help 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