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 Saravia95 · Apr 18, 2014 at 03:46 AM · animationerrorscreenshot

Animation Scripting C# Error

I have already asked this question, but I really need a through walk through on how to fix it, I would really appreciate this because I am doing self teaching but I cannot figure this error out...

The Error is :

NullReferenceException: Object reference not set to an instance of an object EnemyAnimation.Start () (at Assets/The Game/Scripts/EnemyAnimation.cs:24)

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAnimation : MonoBehaviour
 {
         public Animation _animation;
         // Use this for initialization
         void Start ()
         {
                 _animation = GetComponentInChildren<Animation> ();
                 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;
         }
     
         // Update is called once per frame
         void Update ()
         {
 
         }
 }




Here are some pictures on how I added it to the component on unity. Hopefully it is visible. alt text

Also where it says Animation in the script component, it wont let me add anything, when I go to add something it gives me the options and when I select an animation, it does not update...

unity1.png (133.3 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ohboyotero · Apr 18, 2014 at 05:07 AM

If the line numbers in your posted code match the error, that would suggest that either:

  1. "_animation" is null (meaning that the GetComponentInChildren() call above returned null), or

  2. "_animation[animationToPlay]" is null.

As far as why these are null, that depends on what you're trying to do. Can you explain what you're trying to do with this code? Why are you calling GetComponentInChildren() to find the animation, for instance?

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 Saravia95 · Apr 18, 2014 at 05:22 AM 0
Share

It is so it can play an animation for the zombie I have.

and I am randomizing between 3 animations so that all the zombies are not doing the same animations.

Here is what I am doing:

http://dl.dropboxusercontent.com/u/25260770/UnityLessons/FeuUnity3dLesson9.pdf

It is on page 10.

Please help D:

avatar image ohboyotero · Apr 18, 2014 at 07:19 PM 0
Share

Two things:

  1. Could you add a few lines of code right after "_animation = GetComponentInChildren<Animation>();" on line 10?

    Debug.Log("_animation is: " + _animation);

    Debug.Log("_animation[$$anonymous$$ove1] is: " + _animation["$$anonymous$$ove1"]);

    Debug.Log("_animation[$$anonymous$$ove2] is: " + _animation["$$anonymous$$ove2"]);

    Debug.Log("_animation[$$anonymous$$ove3] is: " + _animation["$$anonymous$$ove3"]);

Run the program and look at the console and figure out if any of those lines print "blah blah is: null." That'll tell you what your problems is.

  1. The code you're following doesn't make "animation" public (in fact, usually the underscore prefix indicates "private member variable"), so you shouldn't be trying to set it from the editor like you're suggesting. You're just going to overwrite it when you call:

    _animation = GetComponentInChildren<Animation>();

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

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

Related Questions

Animation spins wildly after completed 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Animation for weapon plays on game start instead of when i attack 2 Answers

Dataset is null 1 Answer

Animation scripting problems 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